Object Abstraction/Inheritance

I am setting up a content management system and I am using classes to connect to database. The problem I’m having is with abstraction/inheritance (not sure which one it’s called). I have 3 classes: department, content & users. All three of these classes use similar methods except the only difference is the table for which they are pulling from.

Assuming I already have a class set up for database connection and it’s assigned a $database variable, I would like to make one parent class that has all the methods each of the three children will need but each child will declare it’s own table name in a static variable.

[php]
// parent class
class ParentObjects {

 protected function create()
 {
      global $database;
      $sql = "INSERT INTO ".self::$tblName." ("id", "name", "etc") VALUES ("1", "bob", "etc")";
      $database->query($sql);
 }

 // additional methods using the child's table name

}
[/php]

I have a few more methods in the parent class and that is just an example but say I have a User class that uses the create function:

[php]
// user class
class User extends ParentObjects {

 protected static $tblName = "user";

 // additional user only methods

}
[/php]

How could I use the ParentObjects code but without declaring the $tblName variable until the child class? Is there such a thing as child::$tblName? I can’t do User::$tblName because there is also Content and Department.

Also, one of the methods that should be in the parent class is an instantiate method that uses $object = new self; to instantiate an array pulled from the database. I tried setting up the Parent class as an abstract class but you can’t instantiate an abstract class.

You could use get_called_class() like this:

[php]class ParentObjects
{
public static function echo_table()
{
$child = get_called_class();
echo $child::$table;
}
}

class User extends ParentObjects
{
public static $table = ‘user table name’;
}

class Content extends ParentObjects
{
public static $table = ‘content table name’;
}

echo 'User table is: ';
User::echo_table();

echo ’ Content table is: ';
Content::echo_table();[/php]
It will output:

User table is: user table name Content table is: content table name

Just read that instead of:

[php]$child = get_called_class();
echo $child::$table;[/php]
You can now do:

[php]echo static::$table;[/php]

More information: http://php.net/manual/en/language.oop5.late-static-bindings.php

That didn’t really work, still gave me an error Access to undeclared static property

what’s the code your using?

I have public static $tblName which is a string and public static $dbFields which is an array. I tried using the get_called_class() but it gave me an error on the dbFields

This should work:

[php]class ParentObjects
{
public static function echo_fields()
{
echo implode(’, ', static::$dbFields);
}
}

class User extends ParentObjects
{
public static $dbFields = array(‘field1’, ‘field2’);
}

echo 'User fields are: ';
User::echo_fields();
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service