6 ms·
When you think about how dynamic PHP is wouldn't you pretty much end up with the source code and a PHP Interpreter baked together into a binary?
by maxxxxx 8y ago
When you think about how dynamic PHP is wouldn't you pretty much end up with the source code and a PHP Interpreter baked together into a binary?
- fiddlerwoaroof 8y agoCommon Lisp is more dynamic than PHP yet is still compiled to reasonable assembly.
- jorams 8y agoCommon Lisp is much more dynamic in some ways, but much less dynamic in others. In PHP you can't redeclare functions or classes, while in Common Lisp you can. On the other hand, in PHP you can manipulate the current scope and make variables appear out of thin air, but you can't do stuff like that in Common Lisp. For example in PHP: $a = $_GET['greeting'] ?? 'hello'; eval('echo $a;'); // => hello, or an error or anything else if there is a query parameter called greeting. $c = [ $a => eval('return "hi";') ]; extract($c); echo $hello; // => hi, or an error or anything else if there is a query parameter called greeting. While in CL: (let ((a "hello")) (eval 'a)) ; => debugger invoked, the variable A is unbound. A CL compiler can make a lot of assumptions about the local environment, and only has to account for real dynamicity in the global environment. For PHP it's basically the other way around. Since most optimizations seem to be relatively localized, that might be a real issue. But then, of course, a good compiler could fall back to unreasonable assembly wherever such dynamic features are used.
- deleted 8y ago[deleted]
- fiddlerwoaroof 8y agoYou can't manipulate the lexical environment using the standardized language, but sbcl and other lisps provide libraries for programatic manipulation of the lexical environment.
- fiddlerwoaroof 8y agoConceptually, you just need to add an extra layer of indirection to local variable lookup.
- lispm 8y agoBut we can use dynamic binding: CL-USER 29 > (let ((a "hello")) (declare (special a)) (eval 'a)) "hello"
- tyingq 8y agoFrom his GitHub page: "Future Work - Right now, this only supports an EXTREMELY limited subset of PHP. There is no support for dynamic anything. Arrays aren't supported. Neither Object properties nor methods are supported. And the only builtin functions that are supported are var_dump and strlen." https://github.com/ircmaxell/php-compiler/blob/master/README.md https://github.com/ircmaxell/php-compiler/blob/master/README... He basically has avoided that road thus far :) I think you're right though. Things like eval() and ${$deref-me-at-runtime} aren't going to be easily compiled.
- thaumaturgy 8y agoThere are many who would argue that removing those features from PHP would be a massive improvement.
- gchamonlive 8y agoReplacing php entirely would be a massive improvement. I only had sorrows with it, the kind of language that is painful to write all the way through.
- DCoder 8y agoReplacing a language does not make all the code written in it go away, though. PHP has its tentacles all over the web with WordPress, Drupal, Magento, etc. and those will not be replaced any time soon.
- gchamonlive 8y agovery true, and the main reason why it is still used. Still, does not disprove what I said, that replacing PHP would be an improvement. A really tough one in many cases, but still an improvement.
- tyingq 8y agoFB is replacing PHP with hack. Nobody outside of FB seems terribly interested now that it won't run existing PHP.
- narrator 8y agoIf they let you call a function to eval a dynamic variable containing php code then yes, but that's generally not a great programming practice from a security and performance perspective.
- maxxxxx 8y agoIt's not a great practice but super common. I have seen code like $i = "j": $$i = 3; WTF?
- mattigames 8y agoYeah, I would never hire someone using dynamic variables, any occurrence of "$$" is a no-no and just denotes poor engineering.
- maxxxxx 8y agoI think if PHP is the only language you know this pattern probably feels extremely elegant. The JS guys also like to do stuff like this.
- CzechTech 8y ago> The JS guys also like to do stuff like this. Harsh, but fair.
- mattigames 8y agoJust wait until you meet dynamic dynamic variables, nothing more elegant than that... $x=0;$a='x';$b='a'; echo $$$b;
- duncan-donuts 8y agoHaven’t seen that one in the wild. Can that chain just keep going?!
- 8y ago
- rkeene2 8y agoNot necessarily, the TclQuadCode [0] compiler which compiles Tcl, which is even more flexible than PHP, into native machine code handles a very large amount of cases. [0] https://core.tcl.tk/tclquadcode/dir?ci=trunk https://core.tcl.tk/tclquadcode/dir?ci=trunk
- pbiggar 8y agoThat's how we handled it in phc, for exactly that reason. Turns out was also useful for simple things like "what is the value of this float" which changed between different versions. Also gets you the standard library. Detailed discussion here: https://paulbiggar.com/research/#sac-2009 https://paulbiggar.com/research/#sac-2009
- TazeTSchnitzel 8y agoIt depends on the code. You can write PHP code with all types either explicitly specified or inferable and no dynamic variable fun without it being too unidiomatic.