3 ms·
> Do you mean problems with the way Javascript implements lexical scope (no "let" blocks until recently, hoisting, global-by-default, etc.) or problems with lex
by trebor 13y ago
> Do you mean problems with the way Javascript implements lexical scope (no "let" blocks until recently, hoisting, global-by-default, etc.) or problems with lexical scope in general?
In general. It's powerful to be able to reach out of your immediate scope to access something outside it, but then everything from the defining scope must be retained for later. Because of variable variables, which can be either wickedly difficult to debug or incredibly handy, virtually anything from the defining scope could be accessed in a way the interpreter couldn't predict.
In some ways, I can already see the mess that could ensue. Which is precisely I appreciated the "use ()" syntax.
> I wasn't saying that a function property should act like a method semantically (ie. be called with an implicit $this argument), only that it should be possible to call it like a method syntactically, ie. the syntax "A(B)" should call "A" with arguments "B". Note that it's not just functions; quux could be anything callable.
I guess I can see the value in that. However, that leads to ambiguity during debugging, especially if you've included a dependency and haven't thoroughly vetted it. In that case, you need some XDebug magic to know if $foo->quux was a closure, callable array or string, invoke-able object, or a method. Then you need to determine its call semantics, etc.
I don't think "anything callable" is appropriate. But I do think anonymous functions, closures, and invoke-able objects are. You could then make strings and arrays invokable by wrapping them in a class, but without trying to execute an erroneous string or array.
> I'm pointing out what could work, and make PHP much nicer.
Yeah, your suggestions could work. Whether it would make PHP better or worse depends on the programmer, and the change's second-order effects on how the object model works. But I'm skeptical of its benefit. It would probably wind up muddying the execution logic till at least 1 patch level further. I'd have to see some real-world examples of how this would be useful to fully convince me.
Call me paranoid if you wish, but I wouldn't want a novice programmer stumbling on that change by accident. I can already envision the spaghetti they'd use it to write, then we'd have to come bail them out. ;)