Page 1 of 1

Scoping issues (ZF)

Posted: Sun Jan 20, 2008 2:40 pm
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?

Re: Scoping issues (ZF)

Posted: Sun Jan 20, 2008 2:49 pm
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.

Re: Scoping issues (ZF)

Posted: Sun Jan 20, 2008 2:58 pm
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.

Re: Scoping issues (ZF)

Posted: Mon Jan 21, 2008 7:20 am
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?