2011/6/8 Christian Kaps <christian.k...@mohiva.com> > On Wed, 8 Jun 2011 08:57:48 -0300, Felipe Pena wrote: > >> Hi, >> >> 2011/6/8 Christian Kaps <christian.k...@mohiva.com> >> >> Hi, >>> >>> >>> what happens if I use this code. >>> >>> class Foo { >>> >>> public $bar; >>> >>> public function __construct() { >>> >>> $this->bar = array($this, 'baz'); >>> $this->bar(); >>> } >>> >>> public function bar() { >>> echo 'bar'; >>> } >>> >>> public function baz() { >>> echo 'baz'; >>> } >>> } >>> >>> new Foo(); >>> >>> What is the output of this snippet? >>> >>> Are there the same rules as for closures? >>> >>> Christian >>> >>> >>> Yes, the same rules. >> > > Hi, > > I think for the sake of consistency it should be possible to use the > following code. > > class Bar { > > public function __construct($dispatcher) { > > $dispatcher->addEventListener('onUpdate', $this->onUpdate); > } > > public function onUpdate() {} > } > > If a property $onUpdate exists then it will be ignored. The same rules as > for Closures or for array callbacks. > > > Christian > > > >
It works in the same way: class foo { public function __construct() { $this->bar = function () { return 1; }; // $this->bar(); // error $x = $this->bar; $x(); // ok $this->bar = array($this, 'baz'); // $this->bar(); // error $x = $this->bar; $x(); // ok } public function baz() { echo 'baz'; } } -- Regards, Felipe Pena