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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt
--
Regards,
Felipe Pena
That can lead to quite a bit of simplifications in code where you now have to check for is_array/is_callable/instanceof Closure and such. I like it.
On Sun, 5 Jun 2011 12:52:45 -0300
Felipe Pena felipensp@gmail.com wrote:
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 forcall_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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt--
Regards,
Felipe Pena
2011/6/5 Benjamin Eberlei kontakt@beberlei.de
That can lead to quite a bit of simplifications in code where you now have
to check for is_array/is_callable/instanceof Closure and such. I like it.
Exactly, and since our current $x = 'hello::world'; $x(); doesn't support
method calls, the array one can help on this just like the call_user_func()
approach with arrays.
<?php
class Hello {
static public function world($x) {
echo "Hello, $x\n";
}
}
function hello_world($x) {
echo "Hello, $x\n";
}
$callbacks = array(
array('Hello', 'world'),
function ($x) { echo "Hello, $x\n"; },
'hello_world'
);
foreach ($callbacks as $k => $callback) {
if (is_callable($callback)) {
$callback($k);
}
}
Output:
Hello, 0
Hello, 1
Hello, 2
--
Regards,
Felipe Pena
I consider this an improvement in terms of consistency w.r.t.
callbacks, so +1 from me, good job!
Best,
2011/6/5 Benjamin Eberlei kontakt@beberlei.de
That can lead to quite a bit of simplifications in code where you now have
to check for is_array/is_callable/instanceof Closure and such. I like it.Exactly, and since our current $x = 'hello::world'; $x(); doesn't support
method calls, the array one can help on this just like thecall_user_func()
approach with arrays.<?php
class Hello {
static public function world($x) {
echo "Hello, $x\n";
}
}function hello_world($x) {
echo "Hello, $x\n";
}$callbacks = array(
array('Hello', 'world'),
function ($x) { echo "Hello, $x\n"; },
'hello_world'
);foreach ($callbacks as $k => $callback) {
if (is_callable($callback)) {
$callback($k);
}
}Output:
Hello, 0
Hello, 1
Hello, 2--
Regards,
Felipe Pena
--
Etienne Kneuss
http://www.colder.ch
+1, very good job!
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 forcall_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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt--
Regards,
Felipe Pena
--
Pierre
@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
+1, very good job!
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 forcall_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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt--
Regards,
Felipe Pena--
Pierre@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
This is a very good idea.
--
Marcel Esser
VP/Engineering, CROSCON
Washington, D.C. Office
@: marcel.esser@croscon.com
office.us +1 (202) 470-6090
mobile.us +1 (202) 250-1988
mobile.de +49 (0)176 391 226 23
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 forcall_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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt--
Regards,
Felipe Pena
+1
Tyrael
Hi!
So, I wrote a patch [2] that allow such behavior to be consistent with
arrays. See some examples:
Looks good. Only question I have is that we seem to have that code
(calling a function based on variable) in two places instead of one, I
wonder if it's necessary and if we could unify them...
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi,
2011/6/5 Stas Malyshev smalyshev@sugarcrm.com
Hi!
So, I wrote a patch [2] that allow such behavior to be consistent with
arrays. See some examples:
Looks good. Only question I have is that we seem to have that code (calling
a function based on variable) in two places instead of one, I wonder if it's
necessary and if we could unify them...
We have the code to initialize the call from a object variable, and string
variable (function only) in this exact opcode ZEND_INIT_FCALL_BY_NAME, which
now treat the array case as well, there is no other place doing such stuff.
--
Regards,
Felipe Pena
Hi!
We have the code to initialize the call from a object variable, and string
variable (function only) in this exact opcode ZEND_INIT_FCALL_BY_NAME, which
now treat the array case as well, there is no other place doing such stuff.
What about call_user_func()
implementation? It must be doing pretty much
the same thing.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
2011/6/5 Stas Malyshev smalyshev@sugarcrm.com
Hi!
We have the code to initialize the call from a object variable, and string
variable (function only) in this exact opcode ZEND_INIT_FCALL_BY_NAME,
which
now treat the array case as well, there is no other place doing such
stuff.What about
call_user_func()
implementation? It must be doing pretty much
the same thing.
- We do not use zend_fcall_info stuff in the VM (which zend_is_callable
works in) - We have to use zend_do_fcall_common_helper instead of
zend_call_function() in the VM
--
Regards,
Felipe Pena
+1
~Hannes
Hi!
- We do not use zend_fcall_info stuff in the VM (which zend_is_callable
works in)- We have to use zend_do_fcall_common_helper instead of
zend_call_function() in the VM
Yes, I know, I just have a feeling we have two pieces of code doing the
same in different way. But I think your proposal is good, if we figure
out later how to unify it we can do it.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
-----Original Message-----
class Hello {
public function world($x) {
echo "Hello, $x\n"; return $this;
}
}$f = array(new Hello, 'foo');
$f();
Am I the only one who doesn't understand what this one is supposed to do..?
Zeev
-----Original Message-----
class Hello {
public function world($x) {
echo "Hello, $x\n"; return $this;
}
}$f = array(new Hello, 'foo');
$f();Am I the only one who doesn't understand what this one is supposed to do..?
Hello::World(), like what you do in call user func, or similar to.
--
Pierre
@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
Hi,
2011/6/5 Zeev Suraski zeev@zend.com
-----Original Message-----
class Hello {
public function world($x) {
echo "Hello, $x\n"; return $this;
}
}$f = array(new Hello, 'foo');
$f();Am I the only one who doesn't understand what this one is supposed to do..?
If you are refering to the 'foo' method and the lack of arg, it's was a
typo...
The right one:
<?php
class Hello {
public function world($x) {
echo "Hello, $x\n"; return $this;
}
}
$f = array(new Hello, 'world');
$f('devs');
--
Regards,
Felipe Pena
Ok, that makes much more sense. Given how everyone loved it I was beginning to wonder whether I’ve become too old to understand simple pieces of code ☺
Zeev
From: Felipe Pena [mailto:felipensp@gmail.com]
Sent: Sunday, June 05, 2011 11:02 PM
To: Zeev Suraski
Cc: internals
Subject: Re: [PHP-DEV] $arr = array('Hello', 'world'); $arr();
Hi,
2011/6/5 Zeev Suraski <zeev@zend.commailto:zeev@zend.com>
-----Original Message-----
class Hello {
public function world($x) {
echo "Hello, $x\n"; return $this;
}
}$f = array(new Hello, 'foo');
$f();
Am I the only one who doesn't understand what this one is supposed to do..?
If you are refering to the 'foo' method and the lack of arg, it's was a typo...
The right one:
<?php
class Hello {
public function world($x) {
echo "Hello, $x\n"; return $this;
}
}
$f = array(new Hello, 'world');
$f('devs');
--
Regards,
Felipe Pena
Hi,
recently I was surprised this didn't work. so +1 from me.
johannes
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 forcall_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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt
Hi Felipe,
I like the idea. It makes indirect method calls less expensive.
I would add a hint to specializer, to eliminate small overhead for
regular function calls.
} else if (OP1_TYPE != IS_CONST &&
EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY) &&
zend_hash_num_elements(Z_ARRVAL_P(function_name)) == 2) {
Thanks. Dmitry.
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 forcall_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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt
Hi,
2011/6/6 Dmitry Stogov dmitry@zend.com
Hi Felipe,
I like the idea. It makes indirect method calls less expensive.
I would add a hint to specializer, to eliminate small overhead for regular
function calls.} else if (OP1_TYPE != IS_CONST &&
EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY) &&
zend_hash_num_elements(Z_ARRVAL_P(function_name)) == 2) {Thanks. Dmitry.
Dmitry:
Oh, right. I added the check for OP2_TYPE != IS_CONST, thanks for reviewing.
Christopher Jones:
Alright, I'll write a page in the wiki for the record.
All:
Committed in 5.4 and trunk now.
Thanks for the comments, folks! :)
--
Regards,
Felipe Pena
<3
David
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 forcall_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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt--
Regards,
Felipe Pena
+1 , nice job
Julien
On Mon, Jun 6, 2011 at 12:53 PM, David Zülke
david.zuelke@bitextender.com wrote:
<3
David
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 forcall_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?
[1] - http://bugs.php.net/bug.php?id=47160
[2] - http://felipe.ath.cx/diff/fr47160.diff
phpt: http://felipe.ath.cx/diff/fr47160.phpt--
Regards,
Felipe Pena
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 forcall_user_func()
-like functions.
I like it.
I also think we should implement "callable" typehint.
A lot of framworks are now adopting Closure as a typehint for that,
which is annoying due to the fact you have to wrap anything else in a
closure just to pass that typehint.
Essentially the callable typehint would be the same as is_callable($arg, true);
That means I can pass in any string with a function name, an array
(with class+methodname), and a closure.
-Hannes
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 forcall_user_func()
-like functions.I like it.
I also think we should implement "callable" typehint.
A lot of framworks are now adopting Closure as a typehint for that,
which is annoying due to the fact you have to wrap anything else in a
closure just to pass that typehint.
Agreed. It's even more problematic because the manual clearly states
that the "Closure" class is considered an implementation detail, and
should not be relied upon. As such, if the implementation changes in the
future, all those frameworks/libraries will need to refactor.
And for those that try to do it "right," they need to jump through a
fair bit of logic (test for object or string, test for array) in order
to hand off execution properly.
Essentially the callable typehint would be the same as
is_callable($arg, true);That means I can pass in any string with a function name, an array
(with class+methodname), and a closure.
+1.
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
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 forcall_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 | matthew@zend.com
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
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 forcall_user_func()
-like functions.
[...]
Any thoughts?
Felipe,
How about collating the emails and examples and creating an RFC for it?
Chris
--
Email: christopher.jones@oracle.com
Tel: +1 650 506 8630
Blog: http://blogs.oracle.com/opal/