Jump to content

Archived

This topic is now archived and is closed to further replies.

shazam

Need help w/ OO Script

Recommended Posts

Hell All,

I should probably start by saying I am a PHP hack at best and very new to OO programming.

Anyhow, I am trying to write a class that extends an existing class.  The original class seems to work fine on it's own but I get the following error in my apache log file when trying to access a class that extends it (of course, the script fails):

[code]
[Sun Oct 01 21:30:34 2006] [error] [client ***] PHP Fatal error:  Call to a member function getRow() on a non-object in C:\\***\\***\\***\\includes\\Asset.class on line 180, referer: http://***/***/asset.php?action=new
[/code]

[b]Environment:[/b]
[list]
[*]Windows XP
[*]Apache 2.0.55
[*]PHP 5.1.1
[/list]

Here is the code I am using to access the extended class:

[code]
$server        = new AssetConfig();
$asset_data = $server->GetAssetData($asset_id);
[/code]

Here is the code of the extended class:

[code]
<?php

require_once 'Asset.class';
require_once 'Template.class';
require_once 'variables.inc';

class AssetConfig extends Asset {

  var $asset_data;
 
  function AssetConfig() {

  }
 
  function GetAssetData($id) {
   
    $this->asset_data = GetAssetDetail($id);
   
    return $this->asset_data;
   
  }

}


?>
[/code]

Here is the code of the parent class (relevant parts only):

[code]
<?php

class Asset {

  var $dsn;
  var $db;
  var $id;
  var $result;
  var $duplicate;
 
  function Asset() {
   
    $this->dsn = $GLOBALS[dsn];
   
    $this->db =& DB::connect($this->dsn);
   
    if (PEAR::isError($this->db)) { die($this->db->getMessage()); }
   
  }

  function GetAssetDetail($id) {

    $query = "SELECT
               asset_status.status_name AS asset_status,
               asset_type.type_name AS asset_type,
               location.location_name AS asset_location,
               asset.asset_serialnum AS asset_serialnum,
               asset.asset_assettag AS asset_assettag,
               asset_po.po_number AS asset_po,
               environment.environment_name AS asset_environment,
               asset.asset_snmp AS asset_snmp,
               asset.asset_name AS asset_name
             FROM
               asset
             INNER JOIN
               asset_status
             ON
               asset.asset_status=asset_status.status_id
             INNER JOIN
               asset_type
             ON
               asset.asset_type=asset_type.type_id
             INNER JOIN
               location
             ON
               asset.asset_location=location.location_id
             INNER JOIN
               environment
             ON
               asset.asset_environment=environment.environment_id
             INNER JOIN
               asset_po
             ON
               asset.asset_po=asset_po.po_id
             WHERE
               asset.asset_id=$id";

      $this->result =& $this->db->getRow($query, array(), DB_FETCHMODE_ASSOC); // <-- This is line 180

      if (PEAR::isError($this->result)) { die($this->result->getMessage()); }

      return $this->result;
  }

}

?>
[/code]

It seems that the object $this->db is not being created by the constructor 'Asset' when accessd by my AssetConfig class.

I've been googling & banging my head for some time.  Can anyone point me into the right direction?

Thanks!

- Brian

Share this post


Link to post
Share on other sites
Hi

I haven't used the extend method before - but in this case i would do the following:

class AssetConfig {  // removed extend

  var $asset_data;
 
  function AssetConfig() {

  }
 
  function GetAssetData($id) {
   
    $this->asset_data = new Asset;
    $this->asset_data->GetAssetDetail($id);

  }

}

Dont know if thats any good to you...

Share this post


Link to post
Share on other sites
Thanks thedarkwinter,

I was thinking of that.  But I was under the impression that by extending a class, you automatically have access to all methods of the parent.  Perhaps there is another step necessary to initialize the parent class' constructor?

- Brian

Share this post


Link to post
Share on other sites
when you extend a class, to specify calling a parent function, you have to use the parent:: distinction. try using this instead:
[code]
<?php
class AssetConfig extends Asset {

  var $asset_data;
 
  function AssetConfig() {

  }
 
  function GetAssetData($id) {
   
    $this->asset_data = parent::GetAssetDetail($id);
   
    return $this->asset_data;
   
  }

}
?>
[/code]

notice that this is only necessary when you have overloaded the parent function. if you are extending the class and not overwriting the functions, you would have normal access to them via $this->.

hope this helps

Share this post


Link to post
Share on other sites
Well, I guess I didn't google hard enough...

According to the manual:

[code]
Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
[/code]

So I added parent::Asset(); to the child's constructor:

[code]
 function AssetConfig() {
   parent::Asset();
 }
[/code]

Everything is woking well now.  Sorry for the spam.

- Brian

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.