Matthew Ratzloff wrote:
This would be a nice idea for a generalized component that would apply to
all classes, but unfortunately you can't do it.

For example, this doesn't work:

$object = new Zend_Delegate::getClassName('Zend_Example');

It has to be in two steps:

$class  = Zend_Delegate::getClassName('Zend_Example');
$object = new $class;

And it's downright impossible for static classes unless you use
call_user_func() or call_user_func_array():

$class = Zend_Delegate::getClassName('Zend_Example');
call_user_func(array($class, 'method')); // Works
$class::method(); // Syntax error

There was no intention of it being used for static classes.
It is purely a generalisation or a standard practice for classes
that instantiate objects, and allow the class of these new objects
to be changed. Zend_Db_Table_Abstract has these methods that
manage this:

 setRowClass();
 getRowClass();
 setRowsetClass();
 getRowsetClass();

Also, Zend_Db_Adapter_Abstract can currently generate Zend_Db_Profiler
and Zend_Db_Select objects, but provides no way of selecting alternative
implementations or subclasses of these.

My proposal is to provide a generic way of allowing any class that
has this behaviour to allow the user to set the class of these
objects before they are instantiated.

It's all about instantiation, so static access isn't an issue.

If we went along the global registration route,
using Zend_Db_Adapter_Abstract for example:

    public function select()
    {
        return new Zend_Db_Select($this);
    }

becomes:

    public function select()
    {
        $class = Zend_Factory::getClass(__CLASS__, 'select');
        return new $class();
    }

Then we can register an alternative/subclass of Zend_Db_Select using
something like:

    Zend_Factory::setClass('Zend_Db_Adapter_Abstract', 'select',
        'MyAlternativeSelect');

then everytime we call $adapter->select(), an instance of
MyAlternativeSelect is returned rather than Zend_Db_Select.

This would eliminate the need for get/set*Class() style methods in
each class.

- Mark.

Reply via email to