A PHP Compiler, a.k.a. the FFI Rabbit Hole
blog.ircmaxell.com
blog.ircmaxell.com
> A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another.
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. CL-USER 29 > (let ((a "hello"))
(declare (special a))
(eval 'a))
"hello""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...
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.
1. We have to criticise the tools we use. That is the only way we improve. If people had that mentality of not criticising a tool because of how popular it is, we would be stuck with cobol for life
2. Amount of code release has extremely little to do with a language being good or bad, just with its popularity. And it is hard to move away from a language once you have a solid base. Still the language flaws are right there and if for any reason you have to touch the code base, being for security upgrade, refactor or feature implementation, there is a potential for a lot of grief just because of the tool someone decided to use for the job.
3. What was really that about hipster programmer? It has been a while since PHP stopped being the mainstream language for backend web development so that really makes little sense.
Your comment wasn't constructive criticism, it was kvetching.
And we are stuck with COBOL for life. I guarantee you that every financial transaction you make gets touched by some COBOL somewhere. One takeaway from this might be, "the horrors! Organizations should be spending more money rewriting things in the latest technologies!" Another takeaway from this might be, "COBOL was so revolutionary for its time that it enjoyed massive adoption three decades ago, and it's still good enough that it isn't worth the cost to replace it in many cases."
You may not enjoy working with it. I've worked with it, I didn't love it either. But it is really, really good at what it does and it doesn't suffer any of the issues plaguing software development in more modern technologies.
> Amount of code release has extremely little to do with a language being good or bad, just with its popularity.
The empirical view would be that truly crappy things don't tend to get both the adoption and staying power of something like PHP.
> if for any reason you have to touch the code base, being for security upgrade, refactor or feature implementation, there is a potential for a lot of grief just because of the tool someone decided to use for the job.
And this is unique to PHP? I've worked in so many different languages and architectures I actually can't ever remember all of them off the top of my head. They all have warts. It's possible to build fragile, incomprehensible code in all of them.
PHP's greatest issue is its accessibility. It is so easy to get started with PHP that for over a decade it was the first language for most web developers. That has led to a lot of really awful code being published, coupled with a lot of really awful, stale documentation scattered around the web.
> 3. What was really that about hipster programmer? It has been a while since PHP stopped being the mainstream language for backend web development so that really makes little sense.
If you measure by "technologies mentioned in HN headlines", maybe. Other sources find that PHP is still used by around 79% of the web (https://w3techs.com/technologies/details/pl-php/all/all). The yearly usage statistics graph is hilarious: https://w3techs.com/technologies/history_overview/programmin...
There are intelligent, reasoned criticisms of PHP from smart people and I enjoy reading those. Same as similar criticisms of Python, or Node.js, or Elegant Pootwaddle. But most of the time, "PHP is bad" is just a low-effort comment people make hoping to get a few nods from the internet.
Conjecture: the average Wordpress site is longer-lived than 50% of the startups launched through YC.
What I meant with cobol is that we could well be using it today in all day applications if we didn't search for better alternatives for security-sensitive applications on the web
> it is really, really good at what it does and it doesn't suffer any of the issues plaguing software development in more modern technologies.
could you provide examples? Because I could really use a better approach to developing in PHP and end my grief where I work.
> The empirical view would be that truly crappy things don't tend to get both the adoption and staying power of something like PHP.
that is a naive statement that disregards the history of PHP. When it was conceived there wasn't anything much better and people just went with it, and it was a lot convenient and filled a gap that javascript just couldn't fill, that is writing code alongside HTML.
> PHP's greatest issue is its accessibility. It is so easy to get started with PHP that for over a decade it was the first language for most web developers
That is far from its greatest issues. There are a lot of accessible languages out there, that is simply not the case. The problem with PHP is that it is bad designed at its core, and not much can be done without breaking compatibility. The surrounding tools are either really bad, example for refactoring, for intellisense etc. or really expensive (PHPStorm). There is less crap code written in haskell, or rust than in php not because haskell is a niche language, but because the language is designed to make stupid decisions hard, something that can't be said about PHP. The language has to hold the programmers hand sometimes.
Even python for this matter has such an idiomatic approach to language design that it feels just weird to write stuff that is not according to the language style guide, even more if programmers stick with style check tools from the beginning.
>Your comment wasn't constructive criticism, it was kvetching.
> But most of the time, "PHP is bad" is just a low-effort comment people make hoping to get a few nods from the internet.
That should not be an excuse to write equally low effort answers. I didn't really think much of my comment, I was just being sarcastic for the sake of it. It wasn't supposed to be constructive criticism. I mentioned criticism in response to what you said about PHP and it's code lines output per criticism.
But just to be clear, I am not saying anything with ill intentions, take this all with a light heart, no hard feelings.
I know little about version 8 though, but as long as they are not abandoning backwards compatibility it won't make up for the language flaws
$i = "j": $$i = 3;
WTF?
$x=0;$a='x';$b='a'; echo $$$b; $x = 'y';
$y = 'x';
echo $$$$$$$$$$$$$y;
I've tested with 1500 `$` and it still works :) foreach(['a' => closureA, 'b' => closureB, 'c' => closureC] as $name => $toApply) {
$myObject->${convertNameToFieldName($name)} = $toApply($something);
}I hate stuff like that. It breaks more than it helps. It overcomplicates trying to dance your way through reasoning what is actually happening. It’s unreliable and I hate it.
Carry on...
int a = 1; int *b = &a; *b = 2;
If this can be compiled to efficient assembly, I don't think that kind of variable binding presents much more of a challenge.All this is essentially a double hashtable lookup (assuming the lexical environment is implemented as a hashtable, but if it's something else, it's similarly trivial):
environment[environment["i"]] = 3
People have been compiling extremely dynamic languages since the Lisps, at the very least; and, assembly itself isn't really a "statically typed" language.Detailed discussion here: https://paulbiggar.com/research/#sac-2009
This is a compiler. The difference between distributing the output of this and a compiled C or C++ library is basically that this emits machine code that no sane C or C++ compiler would emit today. This does not prevent me from figuring out how the parts you want kept secret work, it only spreads bugs out over more code.
My objection is twofold: first, on practical grounds, this makes producing good bug reports harder. That's because there's more code to prove isn't the true source of the problematic behavior.
The second is aesthetic. If your goal is to keep your secrets, this does not accomplish it.
Apart from that, it seems to be following a similar path, but is a little bit earlier down it. Could probably look at the code we generated and copy some of the solutions.
PHC was also designed to support eval/dynamic features by embedding the PHP interpreter and optimizing around it. Not sure that this implementation is going down that path, which has upsides and downsides, the upside being that eval can be handled. Would be interested to see what he ends up doing.
When I looked into it, it wasn't so much eval as dynamic includes, often used for localization:
$lang = "en";
include("l18n/$lang.php");