Hello!

I'm currently working with sfMixer for the first time. I noticed that
when class A is extended with methods of class B, symfony always uses
the same instance of B, no matter which instance of A.

Let me explain: Think of the class structure as of the following
(sfMixer calls left away intentionally)

class A {}

class B
{
    protected $var;

    public function call($instance)
    {
        $this->var = rand(0,9999);
    }
}

sfMixer::register('A', array('B', 'call'));

When you create two instances of A $a1 and $a2 and execute call() on
those two instances, both will have the same value of $var afterwards,
since they share the same instance of B.

In terms of multiple inheritance, this is not correct. Assuming we want
to inherit class A from class B and class C, this is not possible using
PHP alone. So we use the sfMixer to emulate the multiple inheritance. As
a conclusion, we want every instance of A to have its own properties,
whether inherited or not.

There actually is a solution for this problem: spl_object_hash(). This
function returns a unique hash for every object, that remains the same
during runtime. It could be used to create unique instances of B and C
for each instance of A (presuming that sfMixer registered object and not
class method calls). The only disadvantage is that spl_object_hash()
depends on PHP 5.2.0 (no disadvantage IMO, but speaking of the general
audience maybe...)

A temporary solution for this problem is to have $var as array and
register the objects oneself, but this gets ugly when working with many
different properties:

class B
{
    protected $var1=array(), $var2=array(), ...

    public function setVar1($instance, $value)
    {
        $this->var1[spl_object_hash($instance)] = $value;
    }

    public function setVar2($instance, $value)
    {
        $this->var2[spl_object_hash($instance)] = $value;
    }

    ...
}

Can this fix be implemented in sfMixer although relying on PHP 5.2.0?

Regards


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to