by EGreg · 15y ago · 0 comments
That said, the high art of PHP was always to make really performant apps while having your code as organized as possible. These days, this is being done very well with files, autoloading, and opcode caching. If you do things this way, Rasmus would be happy, and things would be fast.
I am a PHP developer. A lot of what I saw in the past few years was frameworks like Cake and symfony that basically copied Rails or brought some heavy techniques with a lot of overhead. This is not the PHP way. If you want to see a framework that does things in a more PHP way, I would humbly submit my own:
It's open source. It's called PHP On Pie for a reason: because it's easy and it uses real PHP techniques, not tries to emulate Ruby or Python. PHP has its own beauty, such as arrays which can support numeric and string indexes at the same time.
All this said, these days PHP is outdated for large sites. Not because there is a lot of crap written in it -- which I agree with. But because it still works only synchronously, and the culture is too server-heavy. Large sites should be written like this:
Clients (browser, etc.) do most of the interface logic.
Components should render themselves using javascript, and only use the server for data
Server does web services (REST, socket push, etc.)
Server should not have to do things synchronously.
Node.js rocks in this respect. Unlike PHP, I can literally issue 10 independent queries to 10 different databases and combine the results as they come in. In PHP, I would have to issue this stuff sequentially. That's slow.The second thing in PHP is fixable. People should move most of the logic (that doesn't need authentication) to the client side, and then write all their web services in a very simple way. Stop generating all your HTML on the server. Check out
http://weblog.rubyonrails.org/2011/4/18/why-http-streaming
and
http://www.facebook.com/notes/facebook-engineering/bigpipe-p...
Rarely do I actually spit on my keyboard.
Not only do most other languages do associative arrays properly, so, you know, numeric and string indices don't clobber each other, but they also don't explode when indexed with, say, anything but decimal literals. You seriously think this is beautiful behavior?
If you want to see what I mean, just try the framework I built for an hour. It's more "php-like" than the rails clones out there.
The advantage of php arrays (imho) is that you can treat them as associative arrays or not dependening on the situation. The lookup behavior is the same for both though, which is convenient.
i don't know python or ruby enough to compare, but it is nicer than javascript where associative arrays (ie objects) and real arrays are entirely different types.
But no other language I've used would work this way. hash[8] would be the same as hash[010] and hash[0x8], and distinct from hash['8']. In addition, if anybody told you that strings would be interpreted as integers, you'd expect that octal strings (which PHP obviously understands) would be interpreted as integers as well! Instead, only decimal formats are handled.
This pernicious confusion of datatypes in the name of convenience is exactly what makes writing correct PHP code so infuriating.
If you want to put the blame entirely on "bad PHP developers", I have some datetime and object-equality behaviors to discuss.
A claw hammer that occasionally bites your hand off is a bad claw hammer. (Bad! No biscuit!)
Its not proof the language is bad, its proof the poster is no better than the truly bad programmers who use the language he is trying to make fun of.
Actually, while we're on the topic: these should be separate types, for exactly the reasons I outlined in that code snippet. Arrays and hashes have entirely different memory characteristics, access times, growth strategies, and indexing semantics! Trying to smush both models into the same construct is what leads to PHP's befucked implementation of "arrays"/"hashes", which succeed at being neither.
As an example, compare Ruby (an abysmally slow language) to PHP on array iteration--a task in which PHP loses by a huge margin on both memory and time complexity.
1) They don't extend stdClass so they aren't technically objects-- they're a different data type completely (WTF?) 2) Arrays are passed by value and not reference by default. Double WTF. So passing an array to a method call without the & reference operator is inefficient and prone to bugs as noobs don't expect the immutable behavior.
I think copy on write was added in the 5.1 timeframe (around 2005)
What's the difference between PHP's arrays and hashtables/dictionaries/etc in languages like Python, Ruby, and JavaScript?
As a Python programmer (who started with PHP years ago), PHP's arrays now seem sort of muddled or confusing. I prefer Python's dedicated list/dict types (though nothing is stopping you from mixing numeric and string keys in a dict).
I mean beauty in the sense of appreciating something. Just because your wife isn't a supermodel, doesn't mean you cannot find her beautiful and have a good marriage. Once you accept PHP for what it is, you find that there's a certain file-oriented style of coding that is perfectly suited to it, that embraces the unique type of PHP arrays, and that autoloads files when needed.
In PHP, you have an array that is basically associative, BUT which you can iterate in the same order that keys are added. This data structure is very unique and often lets you write less code, since you don't have to store the order separately. It also happens to map to JSON very well. Javascript has something like this too, except there is no guarantee that all browsers will implement properties in the same order, and many people on the internet complain about this sort of thing:
http://code.google.com/p/chromium/issues/detail?id=883
http://code.google.com/p/chromium/issues/detail?id=20144
I have to say, I feel for those guys.
http://ruby-doc.org/core/classes/Hash.html
"Hashes enumerate their values in the order that the corresponding keys were inserted."