Scoping issues (ZF)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Scoping issues (ZF)

Post by Eran »

Is there a way to enforce static property overloading?

I am using the Zend Framework databases classes as the base for database abstraction in my application. I would like to be able to retrieve a table name from its table class statically, without having to instance the class.
As it's structured, the Zend_Db_Table class has the $_name property to declare the table name. I've created an alternative static $_tableName property in my extending class that basically carries the same information.

I tried modifying the constructor in such a way that the new $_tableName property would overwrite the $_name property if is set, allowing me to replace the $_name property with my static $_tableName property without having redundant properties. i.e:

Code: Select all

 
//Adding the static property to Zend_Db_Table
class My_Db_Table extends Zend_Db_Table  
{
       public static $_tableName = null;
       public __construct($config = array())
       {
           if(is_string(self::$_tableName)){
              $this -> _name = self::$_tableName;
           }
           parent::__construct($config);
       }
}
 
//Declaring a concrete table class
class FilesTable extends My_Db_Table 
{
        public static $_tableName = 'files';
}
 
$filesTable = new FilesTable(); //Fails. Throws exception 'Table doesn't exist'
 
To my surprise, the preceeding code fails. Apparently (and when you think about it, rightly so), the constructor which is called on the parent My_Db_Table, identifies the $_tableName property as null and therefor fails to properly set the table name.
Is there a way around this? To still provide static access to the table name without having two properties ($_name and $_tableName) containing the same data?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Scoping issues (ZF)

Post by Christopher »

It probably get the table name from the Db object somewhere. And the Db object does a DESCRIBE to get the table information. So you may have to dig further and override more things. It is pretty tightly coupled.
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Scoping issues (ZF)

Post by Eran »

No, thats not it, I dumped my static $_tableName property at the parent's constructor and it came out null. Since static properties are declared per class, when the parent's constructor is invoked it uses the parent's property value which is a default null value, basically ignoring me overriding it in the concrete class.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Scoping issues (ZF)

Post by Eran »

Well it appears that from PHP 5.3.0 you should be able to do something like get_class($this)::$_tableName... However no such alternatives exist right now. Anybody has any ideas?
Post Reply