Jump to content


Photo

Need help w/ OO Script


  • Please log in to reply
5 replies to this topic

#1 shazam

shazam
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 02 October 2006 - 01:04 PM

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):

[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

Environment:
  • Windows XP
  • Apache 2.0.55
  • PHP 5.1.1

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

$server        = new AssetConfig();
$asset_data = $server->GetAssetData($asset_id);

Here is the code of the extended class:

<?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;
    
  }

}


?>

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

<?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;
  }

}

?>

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

#2 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 02 October 2006 - 01:18 PM

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...
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#3 shazam

shazam
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 02 October 2006 - 01:29 PM

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


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 02 October 2006 - 01:41 PM

when you extend a class, to specify calling a parent function, you have to use the parent:: distinction. try using this instead:
<?php
class AssetConfig extends Asset {

  var $asset_data;
  
  function AssetConfig() {

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

}
?>

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
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 shazam

shazam
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 02 October 2006 - 01:44 PM

Well, I guess I didn't google hard enough...

According to the manual:

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. 

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

  function AssetConfig() {
    parent::Asset();
  }

Everything is woking well now.  Sorry for the spam.

- Brian

#6 shazam

shazam
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 02 October 2006 - 01:46 PM


Thanks obsidian!

- Brian




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users