4 ms·
You can do `$foo('bar')` and `baz('bar')` though. Which is horrible ;)
by chriswarbo 10y ago
You can do `$foo('bar')` and `baz('bar')` though. Which is horrible ;)
- TazeTSchnitzel 10y agoIn fact, come to think of it, you can do `$foo(bar)` and `baz(bar)`, but only because undefined constants evaluate to a string of their name by default. (Yes, it's awful.)
- kazinator 10y agoBut isn't that what you have to do to make baz($foo) work also? I mean, how do you put a value into foo so that baz($foo) calls bar? I'm guessing, using foo = 'bar', no?
- chriswarbo 10y ago> I mean, how do you put a value into foo so that baz($foo) calls bar? I'm guessing, using foo = 'bar', no? You could, but that makes $foo a string, which is horrible. Since PHP has actual function values, it would be much nicer to use them, and hence do: $foo = function($x) { return bar($x); }; This is annoying though, since we're forced to perform an eta-expansion of bar just to avoid the syntax error that `$foo = bar;` gives. It's like writing `0+$n` to avoid getting a syntax error when writing `$n`; completely unnecessary, and most likely a bug in the parser. Unfortunately this isn't always possible either, since it depends on the signature of bar. A more flexible example would be: $foo = function() { return call_user_func_array('bar', func_get_args()); }; This still doesn't preserve things like default arguments though, and we're forced to use a string as a function too (although at least it's encapsulated, so it can't cause us to encounter random 'bar' strings in our context and wonder whether or not they're meant to be strings or functions) :(
- kazinator 10y agoBut we started with the problem baz('bar') being horrible; so can't that be substituted here too then? baz(function($x) { return bar($x) }) it's just verbose and indirect: we're making a wrapper (which looks like it may be an extra object at run-time) just to get around a quoting problem.
- chriswarbo 10y agoYes, that's what I meant when I said: > This is annoying though, since we're forced to perform an eta-expansion of bar just to avoid the syntax error that `$foo = bar;` gives. > It's like writing `0+$n` to avoid getting a syntax error when writing `$n`; completely unnecessary, and most likely a bug in the parser. Maybe we differ on where the problem lies; "a quoting problem" sounds like you think the script is at fault; I would say that the PHP language/implementation is at fault.