4 ms·
It can. Python, for example, builds up classes at runtime. PHP builds up classes at compile time. There are plenty of other examples. PHP is really dynamic
by wvenable 8y ago
It can. Python, for example, builds up classes at runtime. PHP builds up classes at compile time. There are plenty of other examples. PHP is really dynamic through eval() but that's more rare.
Although PHP does support the above syntax, it's actually pretty rare in production code. PHP can be optimized like JavaScript is: perform direct variable/member access but provide a slow path for these kinds of dynamic lookups.
- viraptor 8y agoYou can't just wave away the impact of having eval though. Just the fact that it exists means you need to keep around some supporting state like variable names. You can't do full constant folding and eliminate all intermediate steps for example if there a chance a runtime-created code will reach into your frame and want them back.
- rujuladanh 8y agoSimilar things are done by JS engines and others nowadays. They optimize and keep the original stuff in case assumptions stop holding.
- wvenable 8y agoeval() is scoped to the current block. If you even just ignored JITing any function with eval() in it then you'd still get quite far.
- keithnoizu 8y agoI used to use that pattern $$ frequently for library code.
- EGreg 8y agoI wish PHP had classes spread across several files like ASP.NET.
- bufferoverflow 8y agoYou can do this in PHP: <?php eval('class Hello {public static function you(){echo "Yes!";}}'); Hello::you(); or this: <?php file_put_contents('c.php', '<?php class Hello {public static function you(){echo "Yes!";}}'); require('c.php'); Hello::you(); Thus generating a class at runtime-ish.