Die Flagge des Marasek



Current Texts Comic Imprint Calendar Search PHP-Classes Container-Wizard main.s21


Class Library
Global Politics
World Outlook

Python vs. PHP

Previous: FehlerkettenNext: What makes fanbois so bitter?
Assigned keywords: Programmieren

Whenever there's a news entry concerning PHP on heise, evangelists of Python creep out of their holes to attack PHP and how unprofessional, stupid, unperforming and whatever it is. At some point, I took up the gauntlet and worked myself a little bit into Python in order to better know what they're talking about. Actually, I did quite some work with Python and wxWidgets years ago, but abandoned it, partly due to my back then lack of experience in OOP. Now, with a proper IDE and more experience, it was quite a piece of cake for me, even, yeees, the indentations.

It didn't take long before I was sobered up using Python; it feels like a solid language, but I find some things lacking. Python wasn't like Java for me, which mostly gave me a hard time due to the high level of abstraction and the boiler plate code you have to write all the time, but rewarded me with performance a few magnitudes better than PHP and features I had missed, such as return signatures, typed variables and collections as well as interfaces where you can enforce that classes have to throw certain kinds of exceptions.
But with Python, most of my itches were caused by missing features and awkward concepts. So let's go on.

  • self. Whats the point of that? I don't get it.
  • no type hinting. I was really astonished about that, because I thought that people have so much pointed at Zend laughing until they implemented type hinting in PHP5. And I always imagined that, if it was the year 2002 again, Python people would come up to tell me that PHP had no type hinting.
    The point is that I regard type hinting so important that I try to use objects as often as possible in PHP, so I'll have it most of the time; I'll solve the rest with assertions.
    I could use assertions with Python as well, yet the problem is that I don't get all these nice OOP related stuff. For with PHP, I hint for Interfaces and then give objects which implement that Interface. I'd need to program all this, made worse by another, astonishing fact, which leads me to the next point:
  • no abstract classes, no interfaces. Why? I simply don't get it. And no, using zope.interface or writing a class in which the "abstract" methods throw Exceptions isn't what I call "abstract classes and interfaces".
    Abstract classes are supposed to force me to implement the abstract method once I inherit from them. Interfaces are supposed to enforce behaviour. That's the whole point, and whatever workarounds Python fanbois might come up with, this isn't the real thing.
  • no true private, protected. I'd be ready to overlook this one, yet I didn't like the whole line of thought which came from the fanbois ("you don't need that anyway"). Actually, I am quite strict at using private and protected; and I hardly ever use public. They argue that I could circumvent private by using Reflection, but c'mon.

Some arguments of the fanbois which I find unconvincing:

  • 'everything is an object. Not really something that makes Python bad, but it certainly makes the fanbois look bad. You see, the first thing you have to hear from a Python fanboi when he attacks PHP is "in Python, everything is an object". I asked one what the real advantage of that would be, but he told me that those would be "too numerous to list". Ah yes.
    But I have to admit that I like pure functions sometimes, and I don't see the point in treating "primitives" (string, int, float) as objects. In Python, strings are immutable, so string operations, which would be $str = strtoupper($str) in PHP, are in fact str = str.uppercase(), which is somewhat awkward to me, because I'm accustomed that working with methods on objects usually change them (instead of giving me a clone). This is not something I put my heart in, but it makes me doubt why primitives have to be objects or why it should be an advantage. They just don't fall right into the object scheme. Or consider integers; of course you want to use operators to deal with them, but OOP usually is about methods.
    Because I think that primitives does not fit in well, I'll somewhat prefer them to be primitive datatypes separate from the whole OOP stuff. I'm not too strict on this one, actually, I don't care too much (because the other approach has a right of its own), but I'm not easily swayed by just throwing "everything is an object" at me.
  • support for UTF8 is bad in PHP. Seems right at the first place, because PHP so far natively does not know anything about UTF8. Well, that's not true, the preg-functions do. I'll keep the mb_-functions aside, because they're not exactly native and I haven't allowed zope.interface as well.
    Actually, I started with UTF-8 some time ago and made a total move shortly thereafter. Problem was that I had to tend some EU-wide projects, and since the EU has extended to the east, LATIN 1 isn't enough anymore and dealing with codepages seemed to be far more annoying than UTF-8. I had second thoughts about "PHP not being able to deal with UTF-8", but it turned out that this wasn't a real problem. Most of the time I'm actually glad that PHP gives me the true length of a string as opposed to "what a child would count", and I can't say that I'd use strtoupper too often. Sorting could be an issue, but sorting is database work, so actually I never really missed UTF-8 capabilities. Actually, one could say that UTF-8 is not really an issue the language as such should solve. And even then there is m...but I promised to keep that aside.

At the end, some advantages of Python which I really see:

  • more than just array(). Proponents of Java as well as Python lambast PHP for just having array(), instead of Set, Dictionary, Hashtable and so on. Problem is that array() is that it performs very badly on some purposes, when compared to data structures which serve that specific purpose. Big arrays will just use up to much memory. One might argue that within web application, one will probably never use an array with 500.000 entries, but it limits PHP somewhat and doesn't make it a tool of choice for "real" applications. What I don't get is why they don't offer further data structures; with ArrayAccess and Iterator, it should be possible to implement further structures in fast C code.
  • bytecode. You can have that in PHP as well, at least as far as I know, but not natively.
  • threading. Not possible in PHP. Some of the fanbois say that this is good, because the average PHP programmer is too stupid to handle threading. PHP is still quite web centric, and threading doesn't really make as much sense in web applications.
  • better organized base functionality, more stringency. What you can truly say against PHP is that it is still not very coherent. I'm again and again upset that the core functions do not throw exceptions. I also have to admit that PHP has some awkward caveats (a==0).

To sum it up, Python is a sturdy, good language, but it is not as superior to PHP as some people put it. It is superior in some parts, and inferior in others, so I just don't see a real purpose in switching to Python. On the other side, there's not much purpose in switching to PHP either. Java is another thing, because, although I have to pay tribute to the complexity, I really gain compared to PHP and have new possibilities such as to write GUI applications and Applets.


Please note: comments posted won't be visible immediately, as they will be checked for harmful content.

* Title  
* Nickname  
* Comment