Re: Code References
| From: | Sterling Hughes | Date: | Mon, 19 May 2003 14:42:48 +0000 |
| Subject: | Re: Code References | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-1723@lists.php.net to get a copy of this message | ||
On Mon, 2003-05-19 at 11:28, George Schlossnagle wrote:
> 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.
>
Well, right. As I've mentioned beforehand, speed is really a secondary
factor, although it does exist, and the concept of code references
(passing around function pointers as opposed to function names) would
make php faster anywhere callbacks are used.
But its also a cleanliness and ease-of-use factor. If you look at the
amount create_function() is used in PHP code, its next to nil. With
other scripting languages such features are commonly used (and very
*useful* imho). One could assume this is because PHP users don't need
it, but I think its more due to the way its kludged in (imho again). I
think a clear way of having function pointers, and then a nice syntax
for declaring anonymous subroutines (read: I don't want to stash my code
inside a variable), would be something really useful.
-Sterling
>
--
"The three most dangerous things in the world are a programmer
with a soldering iron, a hardware type with a program patch and
a user with an idea."
- Unknown