[PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
On 2011-06-05, Felipe Pena wrote: > Reading our bug tracker I noticed a good feature request [1] from 2009 > which points to an interesting feature that I think makes sense for > us, since we are now working with $f() using objects and strings, and > the array('class', 'method') is an old known for call_user_func()-like > functions. > > So, I wrote a patch [2] that allow such behavior to be consistent with > arrays. See some examples: > > class Hello { >public function world($x) { > echo "Hello, $x\n"; return $this; >} > } > > $f = array('Hello','world'); > var_dump($f('you')); > > $f = array(new Hello, 'foo'); > $f(); > > All such calls match with the call_user_func() behavior related to > magic methods, static & non-static methods. > > The array to be a valid callback should be a 2-element array, and it > must be for the first element object/string and for the second string > only. (just like our zend_is_callable() check and opcodes related to > init call) > > Any thoughts? Huge +1 from me -- having to do constructs like the following have been a huge headache for me: if (is_callable($callback)) { if (is_object($callback) || is_string($callback)) { return $callback($arg); } return call_user_func($callback, $arg) } This would simplify that tremendously: if (is_callable($callback)) { return $callback($arg); } -- Matthew Weier O'Phinney Project Lead| matt...@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
Hi, Hi all, Reading our bug tracker I noticed a good feature request [1] from 2009 which points to an interesting feature that I think makes sense for us, since we are now working with $f() using objects and strings, and the array('class', 'method') is an old known for call_user_func()-like functions. So, I wrote a patch [2] that allow such behavior to be consistent with arrays. See some examples: class Hello { public function world($x) { echo "Hello, $x\n"; return $this; } } $f = array('Hello','world'); var_dump($f('you')); $f = array(new Hello, 'foo'); $f(); All such calls match with the call_user_func() behavior related to magic methods, static & non-static methods. The array to be a valid callback should be a 2-element array, and it must be for the first element object/string and for the second string only. (just like our zend_is_callable() check and opcodes related to init call) Any thoughts? 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
Hi, 2011/6/8 Christian Kaps > Hi, > > > Hi all, >> Reading our bug tracker I noticed a good feature request [1] from 2009 >> which >> points to an interesting feature that I think makes sense for us, since we >> are now working with $f() using objects and strings, and the >> array('class', >> 'method') is an old known for call_user_func()-like functions. >> >> So, I wrote a patch [2] that allow such behavior to be consistent with >> arrays. See some examples: >> >> class Hello { >> public function world($x) { >> echo "Hello, $x\n"; return $this; >> } >> } >> >> $f = array('Hello','world'); >> var_dump($f('you')); >> >> $f = array(new Hello, 'foo'); >> $f(); >> >> All such calls match with the call_user_func() behavior related to magic >> methods, static & non-static methods. >> >> The array to be a valid callback should be a 2-element array, and it must >> be >> for the first element object/string and for the second string only. (just >> like our zend_is_callable() check and opcodes related to init call) >> >> Any thoughts? >> >> > > 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. -- Regards, Felipe Pena
Re: [PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
On Wed, 8 Jun 2011 08:57:48 -0300, Felipe Pena wrote: Hi, 2011/6/8 Christian Kaps 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
2011/6/8 Christian Kaps > On Wed, 8 Jun 2011 08:57:48 -0300, Felipe Pena wrote: > >> Hi, >> >> 2011/6/8 Christian Kaps >> >> 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
Re: [PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
On Wed, Jun 8, 2011 at 2:46 PM, Felipe Pena wrote: > 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'; > } > } What he meant was passing an existing method as a callback if you don't invoke it, i.e. passing "$this->bar" instead of array($this, "bar"). I don't know how hard it'd be to achieve, but it sounds pretty awesome to me. Cheers -- Jordi Boggiano @seldaek :: http://seld.be/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
On Wed, 8 Jun 2011 09:46:26 -0300, Felipe Pena wrote: 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'; } } OK, my mistake. I thought a property which holds a closure can be called directly. Thanks for clarification. Christian -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
On Wed, 8 Jun 2011 15:39:59 +0200, Jordi Boggiano wrote: On Wed, Jun 8, 2011 at 2:46 PM, Felipe Pena wrote: 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'; } } What he meant was passing an existing method as a callback if you don't invoke it, i.e. passing "$this->bar" instead of array($this, "bar"). I don't know how hard it'd be to achieve, but it sounds pretty awesome to me. Cheers Yep, just what I meant. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
Will this work: array('foo', 'bar')('arg1', 'arg2') ? On Wed, Jun 8, 2011 at 3:48 PM, Christian Kaps wrote: > On Wed, 8 Jun 2011 15:39:59 +0200, Jordi Boggiano wrote: >> >> On Wed, Jun 8, 2011 at 2:46 PM, Felipe Pena wrote: >>> >>> 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'; >>> } >>> } >> >> What he meant was passing an existing method as a callback if you >> don't invoke it, i.e. passing "$this->bar" instead of array($this, >> "bar"). I don't know how hard it'd be to achieve, but it sounds pretty >> awesome to me. >> >> Cheers > > Yep, just what I meant. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Re: $arr = array('Hello', 'world'); $arr();
2011/7/14 Rune Kaagaard : > Will this work: > > array('foo', 'bar')('arg1', 'arg2') > > ? > No, and it isn't supposed to either. -- Regards, Felipe Pena -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php