Re: Code References

From: Date: Mon, 19 May 2003 15:28:40 +0000
Subject: Re: Code References
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-1718@lists.php.net to get a copy of this message
On Monday, May 19, 2003, at 09:56 AM, Sterling Hughes wrote:
On Mon, 2003-05-19 at 11:09, Zeev Suraski wrote:
At 16:34 19/05/2003, Sterling Hughes wrote:
On Mon, 2003-05-19 at 10:55, Wez Furlong wrote:
use perl;
Python, Ruby, LISP, Java, C, whatever, all support this in some form or another (whether it be function pointers or code references.)
And so does PHP, through create_function, using PHPish syntax. create_function is not related in any way to call_user_func(), it's fairly fast, and will be faster. I don't see any additional things that are supported by this syntax, other than a tiny performance gain, that comes at the price of significantly reduced functionality (the code cannot contain anything dynamic, whereas with create_function, it can).
That's why one has eval. Create_function() is a really hacky way of having closures/callbacks. Its not only inefficient, its ugly. create_function() is hardly ever used because its such an inelegant (and inefficient) way of doing things. From a syntax perspective anonymous functions are much clearer (the problem with single quotes is you need to then escape all single quotes).
I agree with you completely on the aesthetics. The quoting makes it as hard to read as a double-quoted perl eval. Zeev is right re: the performance though. The eval is only performed once (though it is of course an eval), which is only marginally more expensive than compiling it completely at script compile time, and that assumes that you always end up compiling it.

« previous php.internals (#1718) next »