Inheritance problem with PHP 5 objects

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am having a problem with inheritance and static properties. I am
writing a data record class object structure for Xoops.

Because the table name is dynamic, I have to set it as a static member
for each class. However I am having trouble finding a way to pass this
static member to the subclass.

for instance,

/************ SAMPLE RECORD CLASS ***************/

class Address_class extends Data_class {
  const TABLE_NAME = 'address';
  public static $x_table_name = '';
  public ID = 0;

  public function __construct($pID){
   $this->ID = $pID;

Address_class::$x_table_name = OPS_DB_PREFIX . '_' .

/************** CORE DATA CLASS ***************/

abstract class Data_class {

  function get(){
    $result = $this->execute_SQL(sprintf("SELECT * FROM %s WHERE ID =
%d LIMIT 1;", self::$x_table_name, $this->ID));
    {--- process result --}

/****************** END CODE ********************/

The problem is that becuase it is static and in the child class, the
value of self::$x_table_name is undefined in the context of the parent
method get(); where because it is a normal property, I can pass ID just

Any thoughts

Re: Inheritance problem with PHP 5 objects

Your approach is all wrong. You are defining the table name as a constant  
and not putting anything in the static variable. You ought to try what I  
have been doing for the past couple of years, which is to set the table name  
within the constructor of the subclass, like this....

require_once '';
class Person extends Default_Table {
    function Person()
        $this->tablename       = 'person';
        $this->dbname          = 'sample';

    } // Person
} // end class

Note that the variables $tablename and $dbname are defined in the superclass  
and do not need to be redefined in each subclass.

Hope this helps.

Tony Marston

Quoted text here. Click to load it

Re: Inheritance problem with PHP 5 objects

That is of course a good and functional solution. I might be just
pushing things too far, but ideally, as the table name is constant for
every record, I'd like the efficiency of being able to store it once,

And beyond that, I'd like to know the answer to the principle behind
the question:

IS IT POSSIBLE for a parent method to access the static fields of a
child class?

Re: Inheritance problem with PHP 5 objects

bingomanatee wrote:
Quoted text here. Click to load it

Not reliably.

The child will always have and know about its parent.  The parent never knows  
about the child and may not have one.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Inheritance problem with PHP 5 objects

aha - I got it!

FYI this class uses the XOOPS libraries to handle data management, so
the XOOPS_DB_PREFIX is a runtime constant. (which is why the table name
is stored in a static, not a const.)

Using this methodology, you don't keep a duplicate of the table name
with each instance of the record, and most of the data handling
mechanisms can be buried in the base Data_class class.

/******** CODE BEGINS ****************/

abstract class Data_class {

  abstract function get_table_name();

  protected function &get_array()
    global $Data_class_conn;
    $sql    =  sprintf(self::SELECT_SQL, $this->get_table_name(),
sprintf(self::WHERE_ID_SQL, $this->ID));
    if (!$result)  return Feedback::db_error($this, 'get_array',
"(object $this->obj_ID) Database error:");
    $array =&
    return $array;
} // end Data_class

  class Dir_class extends Data_class
    public static $x_table_name = '';

    /*********** TABLE NAME ACCESS

   public function get_table_name(){ return Dir_class::$x_table_name; }

} // end Dir_class

Dir_class::$x_table_name = XOOPS_DB_PREFIX . '_' .

Site Timeline