Space Cat, Prince Among Thieves

PHP Has Grown Up, You Should Too

Over the weekend I went to a talk on Scala. The speaker said variety of harsh, inflammatory, and mostly wrong things about PHP and the PHP community.

One such example:

The PHP community doesn't care about things like lambdas, they just care about getting a site up as fast as possible.

This is just rubbish. PHP got the lambda treatment in 2009, while Java received it only months ago. The speaker is a Java developer who walked away from PHP over five years ago. He had not seen it grow up. He doesn't know the current state of things, he is misrepresenting PHP.

Later on, he asked what technology the audience used. I loudly proclaimed PHP, despite the speakers previous grumblings. An audience member in front of me turned around and scoffed, or at least so I thought. I felt at least a little like a martyr at this point and decided to take action. Justice was in order.

After the talk, the "bearded dude" approached me. He had seen the tweet thanks to the hash tag. He apologized and said it was not a scoff, but rather an expression of excitement that someone would bring it up after the speakers treatment of the language. He works in the language some too and was glad to see someone stand up for it. I shook his hand and apologized for the tweet.

Later on he left the following reply:

I had misjudged him, as the speaker had misjudged PHP. Alas the hypocrisy; I was no better than the speaker.

The whole experience got me thinking about why there is still so much FUD and ill will around PHP. After all, we've had many modern niceties for a good while now. The language has matured significantly in the last decade. It has become a modern language that's scalable, fast, and enjoyable, all while still easy to deploy.

Moreover, it has one of the best package managers I've ever used - Composer, and a vibrant and helpful community. Seriously, why all the hate?

The moral to this story is think before you act. Make sure you have the whole picture. I didn't, the speaker didn't. This has been my 2¢ for the day.

Comment by: Michael on

Michael's Gravatar As a php developer who several years ago moved on to Ruby and the like, I was pleasantly surprised when checking out the php scene to find that they had some of the toolsets in php that I liked so much in ruby land. Composer is pretty good for what it does. Laravel is a great framework. I've since done some side jobs in php that were quite fun. I don't know that I could do it every day at work anymore, but that's a flavor issue not a statement about the language.

Comment by: James on

James's Gravatar I've been using PHP since the version 3 days. A lot of the earlier frustrations I had with the language have all but gone. When I look back on code from a while ago, I cringe. I understand that if that's all other's know of PHP, they're probably justified in their judgement. But when looking at the most recent projects and code, I see a thing of beauty. If those same people saw this new state of PHP, I think they may scoff just a bit less.

Comment by: Vlad on

Vlad's Gravatar For enterprise applications, I wouldn't use PHP, for small sites (just as an example) - I wouldn't use Java, and for scripting I wouldn't use either (python does the job well).

It's just a question of the right tool for the right job.

Comment by: Morgan on

Morgan's Gravatar I don't think "has grown up" is the right wording. "Is growing up" may be a bit better. I love PHP, but until I can run a server script for more than an hour without running out of RAM, it hasn't finished growing up.

Comment by: Jesse G. Donat on

Jesse G. Donat's Gravatar I'm curious what version of PHP exactly you are running, because this is another issue thats already fixed. The garbage collection in 5.3 and up is *vastly* improved, improving with each release.

From experience I know that we had a number of Gearman worker scripts in production that ran for well over 3 months uninterrupted before we needed to restart supervisord, and their memory usage was negligible.

This is dependent though on your scripts and components to not themselves have memory leaks, which is often too much to ask PHP developers, as they never expect their code to exist in memory for more than a few fleeting seconds.

Comment by: Ted Wood on

Ted Wood's Gravatar I'm a proud and successful PHP developer. I've built my own web applications framework, with CodeIgniter as the foundation. I'm extremely happy with the capabilities and performance of my closed-source platform, which happily powers all of my clients' sites. PHP doesn't deserve the bad rap that it gets.

Comment by: Ted Wood on

Ted Wood's Gravatar Instead of long-running scripts, I have designed an event+action task manager for my platform. Any application can queue tasks, and then a single heartbeat cron job checks for queues tasks and executes them. This works extremely well and I can set the rate of the heartbeat to whatever is most suitable for each website.

Comment by: Mike on

Mike's Gravatar "PHP got the lambda treatment in 2009, while Java received it only months ago."

Yee, PHP is evolutionary grown and will continue with that. While ohters languages have a design the language PHP adapts plenty different things at some point of it's syntax/core.

Dear author, please Make sure you have the whole picture. ;)

Comment by: Felipe Hummel on

Felipe Hummel's Gravatar I've been using PHP side by side with Scala in a daily basis for the last few months. Indeed PHP has been "growing up" in the past few years, but in my opinion it has so many little problems that makes the experience still annoying (for me).

Lambdas entered in PHP 5.3 but still with that verbose syntax. A lot of idioms lambdas and functional programming favors are just plain painful to use with PHP anonymous function syntax.

Example: I want to get a list of all the ids from some objects.

array_map($array, function ($obj) { return $obj->id; });
Scala: obj => )
Or the more compact version:

Idioms like chaining transformations to an array are simply inviable and you are obliged to put intermediate results in variables:

$oldPeople = array_filter($array, function ($person) { return $person.age > 18; });
$names = array_map($oldPeople, function ($person) { return $person->name; });
array_foreach($names, function ($name) { echo $name; });

array.filter( person => person.age > 18)
.map( person => )
.foreach( name => println(name) )

The Scala code is much cleaner. Even though it is a static typed language (which would normally be more verbose than a dynamic one).

Aside from lambda stuff, there are some things that annoys me in PHP:

- No Set data structure. A lot of things in PHP should use a Set data structure instead of a plain array + in_array(). This problem comes in two parts: (1) the semantic is not correct, the thing you want is not an array of things it is a Set of things; (2) using in_array to simulate a Set is slow (it is O(N) opposed to a normal set implementation with O(1) or O(logN)). You can workaround this limitation with a couple of helper functions (as I did) or classes, but it is a pain that the language does not provide it (I don't recall a mainstream language that does not have Sets).
- Inconsistency in functions. The most notable one, for me, is the array_map(callback, array) versus array_filter(array, callback).
- PHP should embrace OO in its core and transform Array and String in objects. The methods could be simply aliases to the native methods, but would ease some pain anyway.

Despite all that, I still think PHP is a good language to implement stuff. Specially because its so easy to get started and put HTML on the screen.

Anyway, just my 2 cents.

Comment by: justranting on

justranting's Gravatar I work with PHP every day, it is a huge mess. It gets the job done, but for me it is stressful to work with it - the language and most of the projects written with it are so chaotic.

Like everything new gets implemented in some weird backwards way compared to other languages. Latest example of something that will probably be implemented in PHP - return type checking:

function getNumber($args) : int

Like wth? I've worked with many staticly typed languages and the way you do it elsewhere is this:
function int getNumber($args)

Looks more senseful, doesn't it?

We already got type checking for function arguments right now and I like the general idea of having return type checking though. Once we have that though, I bet in 5 years the devs will realise that hey - it would make sense to be able to type hint variable declarations too. Why do this kind of realisations have to take ages, what's with the teasing?

I predict we'll have the possibility of using PHP like a staticly typed language in distant future, I predict we'll then have even more people saying how PHP has finally grown up. Now lets continue hacking stuff together and wait for PHP to mature into a more serious language another decade? Or maybe it would be better/more fun to switch to a language that has been designed logically from day 1?

Comment by: Jesse G. Donat on

Jesse G. Donat's Gravatar

Arguments that PHP should do x because everything else does x are beyond irritating, they're baseless and shallow minded.

If you want to use x, use x, not PHP. PHP isn't a statically typed language, and that is what makes it PHP and not Java or whatever else.

The RFC in question is here and I actually am a big supporter of it.

The suffix type is used by Go and actually makes a lot of sense, specifically when you wan't the same syntax for your anonymous functions as your named functions, something Go does, and something we want for PHP.

The problem with a prefix rather than a suffix when the language is optionally typed rather than strongly typed is that it can't support lambdas.

// Named and Typed
function MyObjectType getObject($args) {

// Typed and anonymous or simply named? The syntax is unclear.
function MyObjectType ($args) {

As opposed to

// Named and Typed
function getObject($args) : MyObjectType {

// Typed and anonymous
function($args) : MyObjectType {

As for the time comment, I direct you to the amazing post Not Implementing Features Is Hard by Robert O'Callahan.

Email address will never be publicly visible.

Basic HTML allowed.