I've been just looking for possiblity to pass method by reference in PHP. Why ? Well, it is possible in Python where everything is an object.And it is sometimes really useful. Imagine that You want to call some particular specific method within some more general object. Imagine that the state of the general one depends on the results of this method. But as I said, implementation of this method may be different for different objects. It is sort of Delegation Pattern where responsibility of implementation certain behaviour is being delegated to some other object.
Of course it is possible to pass a name of a function as a string and or calling user function. But this is not I was looking for.
And again I found usefulness of magic methods in PHP.
class A{
public function say($word){
print $word;
}
}
class B{
/**
* try of calling not existing method on object B
* will execute the magic method
*/
__call($m,$a){
return $a[0]()->$m($a[1]);
}
}
$a = new A;
$B = new B;
// actually I pass object reference
$B->say($a,"Hallo");
Nice ? :) Quite... Of course body of magic __call method could be more sophisticated and more general. But this is just for testing of the behaviour that I was looking for. Anyway, I have found such sophisticated implementation using Reflection. However my apache is always blowing out while trying to call that code :).





