
Announcing a specification for PHP - keso
http://hhvm.com/blog/5723/announcing-a-specification-for-php
======
maaaats
After recently having to work with modern PHP, I have to say a lot of the
criticism of the language is unfounded. It's changed a lot since I first used
it.

But the stdlib is still hard to manage. Different naming conventions,
different order on the parameters for functions that do almost the same thing,
and every function is global. Couldn't they keep all that for backwards
compatibility, but create more sane wrappers and include them as part of the
stdlib? For starters, group them by what they work on (arrays, strings,
numbers etc.) as static functions on some relevant class (
String::str_replace(...) ), and later have more methods one can call on the
objects themselves ( myStr.replace(...) ).

Is this being worked on? Can this spec help with that?

~~~
Arnor
Fun with PHP:

    
    
        $a = 'b';
        $b = 'a';
        echo $a."\n";
        >>> b
        echo $$a."\n";
        >>> a
        echo $$$a."\n";
        >>> b
    

Yaaa

    
    
        $obj = new StdClass();
        $obj->x = 1;
    
        function x_plus_one($obj) {
            $obj->x++;
        }
    
        x_plus_one($obj);
        var_dump($obj);
        >>> object(stdClass)#1 (1) {
        >>>  ["x"]=>
        >>>  int(2)
        >>> }
    

Huh? A local change to the object changed the object outside the local scope?
It must have been passed by reference...

    
    
        function null_the_object($obj) {
            $obj = null;
        }
    
        null_the_object($obj);
        var_dump($obj);
        >>> object(stdClass)#1 (1) {
        >>>  ["x"]=>
        >>>  int(2)
        >>> }
    

Huh? Before when I edited the object within the function, the object I passed
in was edited, what gives?

We don't actually pass objects to reference in PHP, we pass them by "object
reference." But don't worry, you can still null objects that get passed to
you:

    
    
        function null_the_object_2(&$obj) {
            $obj = null;
        }
    
        null_the_object_2($obj);
        var_dump($obj);
        >>> NULL
    

Sure it's not as bad as it used to be. Yes, a disciplined developer can make
good software with it, but it's still a pretty quirky.

~~~
DigitalSea
If you're going to rag on a programming language on a site like Hacker News my
advice would be to make sure you know what you are on about before you do.
There isn't a programming language out there that doesn't have its quirks,
look at Javascript, it has more quirks than you can poke a stick at, but that
doesn't make it a bad language.

In your examples I am not seeing the issue? Am I missing something here? Your
examples work exactly as I would have expected them to, as they would in other
similar languages like Java and C++. Lets stop the hate for the sake of hating
PHP, it definitely has its quirks in relation to methods and order of
parameters in particular, but things are getting better and they're not deal-
breakers for getting things done.

If you want to see how great things are in PHP, I would checkout the Laravel
PHP framework. It is a very well built and robust framework that uses the best
parts of PHP and abstracts some of the worse parts to make the experience of
developing in PHP fun again.

~~~
Pacabel
The presence of some quirks isn't the issue. Of course every programming
language has some.

PHP and JavaScript, however, have unjustifiable quirks with some of their most
basic functionality. We're talking about stuff as basic as their comparison
operators being quite broken, for example. That's where the problem is.
Furthermore, the fact that these issues have been around for well over a
decade or more without being properly addressed just makes the situation even
worse.

Compared to other mainstream languages like C, C++, Java, C#, Python, Ruby and
even Perl, both PHP and JavaScript are quite inferior in many critical ways.
Perhaps things will improve over time, but we really haven't seen that happen
so far.

So let's not pretend that they're "good" just to be politically correct or to
try to avoid offending anyone. PHP and JavaScript aren't good languages, and
they won't really improve until their communities admit that there are some
very serious issues, and then commit to getting them resolved properly and
rapidly.

~~~
Dewie
> The presence of some quirks isn't the issue. Of course every programming
> language has some.

It is a very weird argument that often comes up when discussing programming
languages - "Lang X is weird/stupid in some ways, but no language is perfect,
so it's fine and there is no reason to call it out."

So, because nothing is perfect (almost by definition), it apparently isn't a
big deal that some things are weird/dumb/broken. And let's also just muddy the
waters by implying that, since all languages have imperfections, talking about
some other language's imperfections is just being pointlessly selective (even
though one of the languages imperfections are much more horrible).

It's just overall a non-argument. Maybe the point is to come off as being
"above" the opposite camp (which are: mindless language bashers). But it's
about as intellectually stimulating as mindless language bashing: it doesn't
take much intellectual curiosity or rigour to bash a language, and neither
does being perfectly neutral and non-offensive by throwing around platitudes
like "no language is perfect", "choose the right tool for the job", etc.

~~~
mike_hearn
Exactly. The trend in modern languages is to try and eliminate as many
surprising quirks as possible. If you look at Kotlin, it's basically
advertised as resolving around half of the famous Java Puzzlers. And those
puzzlers were themselves introduced by the authors with the proviso that Java
has remarkably few quirks for something so very large, and that this is/was
tremendous progress.

------
ecaron
I sure hope Sara Golemon gets the recognition out of this that she deserves
(and which the article starts to build.) Having met her at a couple
conferences, she continues to demonstrate a brilliant understanding of what it
takes to build a programming language WHILE being a tremendous advocate and
builder of a community where being a member isn't always... popular.

Rock on, @saramg

~~~
SaraMG
<3

The props go to Joel Marcey, Drew Paroski, and Rex Jaeschke though.

------
kayoone
Recently i feel there even is a kind of PHP renaissance. I think its a good
time to revisit PHP and look at what it has to offer these days. The community
is enormous and open source frameworks like symfony2, zend2 or laravel are
thriving like never before with solid implementations for nearly everything.
With composer there is mature packet management now and in general the code
put out for these kind of projects is of very high quality. And then there is
facebook pushing PHP forward with things like HHVM, however for most stuff i
have worked on, php execution time hasn't really been the bottleneck anyway,
but its nice being able to squeeze out even more speed.

These days i also work a lot in node and angular, but PHP (using symfony) is
still my goto language for a solid REST backend and/or classic static content
based websites.

------
lnanek2
I'm not really a fan of having a second source of truth compared to in depth
material already on php.net. The two will invariably start to disagree and
php.net is much, much better than any spec or documentation I have ever read.
Using Android is a nightmare compared to php.net because every single thing on
php.net has extensive user generated documentation whereas Android is just
some broken official docs. I submitted a bug report with a fix for the Android
docs telling people to make dialogs in a way that simply crashed and didn't
work and it was never merged in for years and years. So official specs tend to
suck.

~~~
Zelphyr
I think a spec benefits language implementors more than it does every-day
users. Sure, it will influence things in every-day PHP programming but
ultimately I think the target audience is different.

Think of it as what the ECMA spec is to the Node.js API docs. Rarely does a
user writing JS code using Node need to reference the ECMA spec (though they
_should_ by all means be familiar with it!) but they almost certainly have the
API docs bookmarked.

~~~
krallja
The ECMAScript spec might be a bad example, simply because Annotated ES5 is so
good. [http://es5.github.io/](http://es5.github.io/)

------
birkbork
This is great news for the PHP community, and I for one applaud their effort.
Contrary to what many HN hipsters seems to believe, PHP is quite a capable
language, and HHVM / Hack is really pushing things forward.

* Hack introduces type hinting, imo the major lacking part in PHP.

* HHVM introduces speed to php. On a personal project calculating perlin noise, I got about 8x speedup on HHVM.

* The specification helps pave the way for more implementations of PHP.

~~~
revscat
> Contrary to what many HN hipsters seems to believe, PHP is quite a capable
> language, and HHVM / Hack is really pushing things forward.

This attitude really frustrates me. I'm a developer with over 20 years of
experience. I don't use PHP because (a) I have had poor experiences with it in
the past, (b) I am enjoying my current stack (Java8/Clojure/Groovy), and (c)
would go with stacks like RoR over PHP if I had to choose, simply because I've
had good experiences with Rails.

You're implication that this has something to do with trendiness is frankly
insulting.

~~~
hamburglar
Agree. PHP started itself out with the handicap of a lot of shitty
misfeatures, and now that they've beaten it into a state that it's _possible_
(though in no way encouraged) to write decent code in it, anybody who doesn't
put on a "PHP: It's not that bad!" party hat gets called a hater.

I'm afraid they're going to have to show me reasons it's actually superior to
other languages before I'm going to jump on the train. This "it's less broken
than it's ever been" stuff isn't going to cut it.

One thing that makes me chuckle every time I decide to give PHP a fair glance
again is "<?php" at the beginning of all the source files. It's hilarious to
me that with all the well understood best practices about separation of logic
and presentation, people are still seriously using a language where you have
to use a little signal to the compiler that means "ok, here's where the HTML
stops and the code begins!" I assume at this point it's a vestigial tail, but
it's just one irritating reminder that this language was originally designed
for half-ass hacks.

~~~
astrange
So, if your PHP programs don't embed HTML in ?><?, and you use a framework
with its own URL routing instead of multiple files, and your framework has
plugin autoloading and initialization on every page request… why are you using
PHP?

~~~
hamburglar
In case that was not a rhetorical question: I'm not using PHP.

------
ChikkaChiChi
PHP is fantastic if you are starting from scratch and you don't necessarily
need any third party libraries. PDO is very well done and the native database
drivers are some of the fastest available.

With a properly tuned nginx/fpm/apc stack, I've been able to deliver solutions
that truly back up the results you see from comparison benchmarks like
Techempower.

In many cases in web development, PHP can be the right tool for the job!

~~~
girvo
With HHVM + nginx you can _really_ fly! Seriously, it's amazing how fast it is
(and the Techempower benchmarks put it at #1 for anything with multiple
requests and data handling!)

~~~
frik
You are spot on, Nginx+HHVM is really fast. I am an early adopter of HHVM
(since Dez 2012) and it really shines on sites with thousends of requests per
seconds. I heard PHP 5.5+ on Nginx (FPM) is fast as well.

And I prefer PHP as I can code websites without the need of frameworks (PHP is
"the framework") with a C/C++ like syntax. The idea of libraries is much older
than frameworks, and in the end libraries are so much more flexible IMHO.

~~~
tomschlick
> And I prefer PHP as I can code websites without the need of frameworks

That either means A) you re-invent the wheel every time you start a project or
B) you have your own implementation of commonly used items (your own
framework)

I personally would rather have a framework vetted & maintained by thousands of
other developers than one I put together myself.

~~~
chriswarbo
To be fair, PHP's stdlib is essentially a Web framework. It comes with
$_SERVER, $_GET, $_POST, header(), etc. built in, which other languages
(Python, Haskell, Perl, etc.) would define in a framework. Hence there's no
need to redefine stuff over and over. Hell, PHP is even a templating language
by default, thanks to its <?php ?> tags.

Of course, this doesn't answer the related questions: is PHP's built-in
framework any good? Is it a good idea to include a Web framework in all
scripts (even non-Web ones)? Is it a good idea to write all application logic
inside giant <?php ?> tags in template files?

------
Aldo_MX
As a discussion of any programming language grows longer, the probability of a
bashing against such language approaches 1.

Any similarity with Godwin's law is mere coincidence.

~~~
krapp
There are languages that everybody hates, and then there are the languages
nobody uses.

~~~
heinrich5991
I believe C++ is _not_ hated universally and is still used like everywhere.

~~~
coldtea
To better word the original saying: "There are languages that the minority of
geeks that like to quarel about languages hates, and there are languages that
nobody uses -- but for both cases, the millions of programmers out there could
not care less".

------
juddlyon
Can someone explain to a layman why this is significant? I'm genuinely
curious.

~~~
ionforce
Hoping to tide you over until a better answer comes along...

Generally when complex programs (like an entire programming language) are
made, there is usually a "specification" of its behaviors. This way, you can
communicate to people, hey, my program does ABC and does NOT do XYZ.

Also, it helps you delineate between bugs and what not. My program does AB
DEF. Is that a bug? Or a feature? What does the specification say?

Not all programs have a specification. It's a bit formal and unnecessary.
Except in a specific scenario where you have multiple/competing
implementations of the same programming language. Now that there are multiple
players, we need to know exactly which behaviors are per the spec and which
behaviors are per the implementation.

All coffee makers must make ABC XYZ. This is the spec.

Sony CoffeeMate makes ABC DEF XYZ. It matches the spec and throws in some
extra features.

Samsung Coffee Xtreme makes ABC PQR XY. This also throws in extra features but
is actually missing one from the spec.

TL;DR it helps a language evolve independently of its implementations (come
the day that a language != its implementation)

~~~
nawitus
Another way to answer this question is that a programming language
specification tells what the language does in the case there's no stack
overflow answer for the question.

------
robert_nsu
This is good and long overdue. I disagree with the claim of it being the
lingua franca of the internet though.

------
mehrdada
> PHP is definitely the lingua-franca of the internet.

Definitely wrong. If there's a lingua-franca of the internet, it's JavaScript.

~~~
jacquesm
The internet does not have a lingua franca. The web does, and it is HTML.

------
k3liutZu
> PHP is definitely the lingua-franca of the internet.

I think they misspelled Javascript.

~~~
TazeTSchnitzel
PHP is _on the server_.

~~~
xutopia
A lot of the sites we frequent have python, ruby or C# on the server.

~~~
TazeTSchnitzel
That's not the point. PHP is the _lingua franca_ of the server-side as it's by
far the most commonly used language and widely understood. Do others exist?
Sure. Just as other languages exist. But PHP's the big one.

------
frik
The PHP grammar in BNF starts at line 10354. It not that extensive as some may
think and rather similar to what Java BNF looks like. The expression statement
is a bit more extensive than in most languages, as PHP also supports the
little known template-style (if elseif endif;):
[http://php.net/manual/en/control-structures.alternative-
synt...](http://php.net/manual/en/control-structures.alternative-syntax.php)

Thanks for the PHP spec.

Btw. GitHub says "Sorry, this blob took too long to generate.":
[https://github.com/php/php-spec/blob/master/spec/php-spec-
dr...](https://github.com/php/php-spec/blob/master/spec/php-spec-draft.md)

~~~
arenaninja
I got the same message on GitHub. Did you checkout the repo in order to read
it?

Personally, this is exciting news, but I'm not sure that PHP is ready for this
after 20 years. There's plenty of cleanup that needs to be done to userland
naming conventions and parameter orders that in all likelihood won't get done
anytime soon to avoid breaking BC

~~~
frik
yes, you can also use the "Download Zip" button there:
[https://github.com/php/php-spec](https://github.com/php/php-spec)

PHP ships with a lot of third party C libraries that are exposed in PHP with
usually their original function names. Object orientation came with PHP 4 and
later with PHP 5 the new better one (5.3+ with namespace support, traits,
etc.).

You can think of PHP as a mixture of C, C++ and Java for web development and
CLI tools. I personally use all four languages, each one for specific purposes
where the fit best. You can find function names like strstr in C, C++ and PHP
(= indexOf in Java and JavaScript) - not a big deal.

~~~
arenaninja
I'm at work so I was looking to peruse the proposal without downloading
things. You can read it from PHP's git mirror: [http://git.php.net/?p=php-
langspec.git;a=blob_plain;f=spec/p...](http://git.php.net/?p=php-
langspec.git;a=blob_plain;f=spec/php-spec-draft.md;hb=HEAD)

I'm not sure that I would call PHP4 objects object oriented, as I remember it,
it was all such a huge hack (down to being able to return null from a
constructor). I only had to maintain some code like that at the tail end of
PHP4's life, so I never found out whether that was how OOP was done in PHP4,
or if it was a hack to simulate OOP in a language that didn't support it.

------
danso
Maybe I'm missing something here...but the OP links to what must be a gigantic
Markdown file that Github will not serve up...Is the spec meant to be in a
giant doc, or will it be split up into smaller sections? This seems like a
perfect use for Jekyll if they're going to publish on Github...

In any case, I did a quick-fork and generated a Github page from the Markdown
file linked to by OP: [http://dannguyen.github.io/php-
spec/](http://dannguyen.github.io/php-spec/)

~~~
jimarcey
Nice. Thanks.

In my mind, there will be multiple renderings of the spec (like what you had
done here). I have plans to split the spec into multiple, smaller .md files
based on topic. The gating factor was managing the internal reference links,
but I can work around that with some tooling. I did not want this to hold up
getting the spec out there though.

------
pbiggar
It took a while, but I read the spec and wrote a critique:

[http://blog.circleci.com/critiquing-facebooks-new-php-
spec/](http://blog.circleci.com/critiquing-facebooks-new-php-spec/) (comments
at
[https://news.ycombinator.com/item?id=8114919](https://news.ycombinator.com/item?id=8114919))

------
joshdance
Rookie question, but what does a language specification for PHP do or mean? I
assume this is good, but don't know why. :)

~~~
x0x0
The obvious thing it does is lays the groundwork for multiple first-class
implementations. As it stands, the program php essentially is the reference:
whatever it does, implementation quirks, bugs, and all, is what php should do.

------
fabriceleal
This is great news, now we just need to lobby for the creation of this
specification in machine readable files, using something like the K Framework,
instead of using the usual wall of text.

[http://www.kframework.org/index.php/Main_Page](http://www.kframework.org/index.php/Main_Page)

------
ZeroGravitas
This should help the PHP-NG project too. I was a bit worried to see they were
doing large refactorings to increase speed when the PHP test suite only has
about 30% code coverage.

------
cottonseed
At first I thought they were announcing a formal specification for PHP. Who's
going to win the race to build the first formally verified blogging platform?

------
kangax
Looks like PHP spec is about twice smaller than ECMAScript 5.1
([http://es5.github.io/](http://es5.github.io/))

~42K vs ~82K words.

~~~
TazeTSchnitzel
The ES5 spec specifies its whole standard library, however ;)

------
SimeVidas
“PHP is definitely the lingua-franca of the internet.” Uhm, no.

------
rafekett
fantastic work that's long overdue. any commitment from the zend team to
implement it? if not it's a specification for HHVM, not PHP.

------
stefantalpalaru
Better late than Python.

------
vithlani
Hahahah.... what a waste of time and resource. FB would rather direct their
resources towards this meaningless crap then run the risk of their top
employees getting bored -- and leaving them.

------
xanth
Javascript is the lingua-franca of the internet not PHP.

------
ck2
Facebook first needs to bring HHVM up to full compatibility before they try
altering the spec.

I also discovered Facebook makes all their "contributors" (anyone who does a
pull request) sign an agreement with them, which seems weird and not cool.
[https://code.facebook.com/cla](https://code.facebook.com/cla)

~~~
dragonwriter
> Facebook first needs to bring HHVM up to full compatibility before they try
> altering the spec.

 _Defining_ a spec (there isn't a pre-existing spec that is being "altered")
is an important part of moving to acheive compatibility, since its how you
know what the target is.

HHVM spawning a spec effort for PHP is a lot like Rubinius spawning RubySpec.

------
spacemanmatt
What's it been, 20 years?

~~~
jaredmcateer
If you read the article you would have known this. It is literally the first
three sentences they cover this.

~~~
baq
smartass, meet smartass.

------
LBarret
A language without a spec ? Damn, php is even worse than I thought. Still, FB
may turn it (with hack and HMMV) into something interesting.

~~~
masklinn
That's not uncommon for implementation-defined languages. Ruby didn't have a
language specification until the community set up rubyspec as it started
getting serious about alternative implementations.

Facebook is apparently getting serious about its own alternative
implementation (or at least intending to be taken seriously) and are thus
stepping in with a spec.

~~~
ksec
Often I wonder if going without Spec is a good thing. Ruby doesn't want a Spec
because Matz said Ruby is an always evolving languages. But the same could be
said about HTML 5 where it does have a living spec.

~~~
dragonwriter
> Ruby doesn't want a Spec

Both RubySpec and the ISO standardization effort suggest that this is less
than true.

> because Matz said Ruby is an always evolving languages.

That's not a reason not to have a spec; that's a reason that the current
bleeding-edge version might diverge intentionally from the spec and that the
spec might trail.

(Or, as in the case of HTML/HTML5, its why you might have both an evolving
target spec, like WHATWG has with the HTML living spec, for language
implementers to develop toward, and a more conservative one, like the W3C
HTML5 spec, that is more geared toward what language users should target.)

Without a spec -- preferably one that is or includes an executable test suite
-- independent implementations are problematic, and multiple independent
implementations that can experiment with different innovations is good for an
"always evolving language".

