Re: Re: $arr = array('Hello', 'world'); $arr();
| From: | Felipe Pena | Date: | Wed, 08 Jun 2011 12:46:26 +0000 |
| Subject: | Re: Re: $arr = array('Hello', 'world'); $arr(); | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-53209@lists.php.net to get a copy of this message | ||
2011/6/8 Christian Kaps <christian.kaps@mohiva.com>
> On Wed, 8 Jun 2011 08:57:48 -0300, Felipe Pena wrote:
>
>> Hi,
>>
>> 2011/6/8 Christian Kaps <christian.kaps@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