PHP Has Grown Up, You Should Too
- Comments:
- 11
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.
To the heavily bearded dude who turned around and scoffed when I mentioned PHP, die in a fire. #minnebar
— Jesse Donat (@donatj) April 12, 2014
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:
@donatj not a scoff. admiration.
— Jeff Mattfield (@jeffmattfield) April 12, 2014
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
Comment by: James on
Comment by: Vlad on
It's just a question of the right tool for the right job.
Comment by: Morgan on
Comment by: Jesse G. Donat on
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
Comment by: Ted Wood on
Comment by: Mike on
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
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.
PHP:
array_map($array, function ($obj) { return $obj->id; });
Scala:
array.map( obj => obj.id )
Or the more compact version:
array.map(_.id)
Idioms like chaining transformations to an array are simply inviable and you are obliged to put intermediate results in variables:
PHP:
$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; });
Scala:
array.filter( person => person.age > 18)
.map( person => person.name )
.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
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
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.