Annoyances with PHP
Writing PHP is a daily occurrence for myself in both studies and for my day job. In fact, thinking about it, I probably write more PHP than I do English - yikes! In this article, I will explain a sample of the issues I encounter with PHP on a daily basis and wish were different. However, I must say that I am not saying I don’t like PHP, in fact its my language of choice for most tasks due to convenience, simplicity and familiarity.
The indirection operator
This is probably the single, most annoying thing about PHP. It uses such an odd combination of -> to indicate a method call on an object:
$object->method();
The reason I find this annoying is that it is very prone to typing mistakes. It requires you to not only type an extra character (compared to other languages) but switch case in the process.
I guess the reason for this is that the common alternative, the dot, is used for string concatenation. And I’m pretty sure (although please correct me if I’m wrong), but PHP cannot do operating overloading. Operating overloading is where the same symbol can be used for different things in different contexts. So for example in Java you can use the + symbol to add two numerical values together, and also use it for string concatenation:
$x = 1 + 2; // result: 3
$x = ‘hello ‘ + ‘world!’; // result: hello world!
I would love to see this operator changes to dot, like just about any other language in existence.
No type hinting for scalar types
While PHP is not a statically typed language, and nor should it be it has recently introduced type hints for methods. However, the odd thing is that this type hinting can only be used with objects or arrays (no int, string etc…). Is it my lack of understanding or does this seem like a serious omission. The following code illustrates type hinting:
public function add(array $data, MyObject $object)
{
//...
}
The above code will cause an error if the method is passed the data parameter with something other than an array and likewise for the object parameter. This reduces all the type checks you have to do in the body of a method. It would be nice if PHP could do this for scalar types too.
Register globals
As a security conscious developer and also a developer that likes to write code that can easily be read register globals is another one of the things annoying about PHP.
For people not familiar register globals is a feature of the PHP interpreter which automatically GET and POST contents as variables in the global scope. So if you had a get parameter called name, there would automatically be a variable in the global scope called $name containing the value.
This presents two core issues. The first is that it can be a security risk if variables are not explicitly set before usage in a PHP program and an attacker could manipulate GET and POST contents to modify program behavior.
The second problem with register globals is that it is not intuitive to a programmer than is new to PHP unless they have knowlegde of the behaviour. They will just see that a variable is being used. Then when they try to find where it is coming from they simply will not find it. Now if this is a particularly complex application with lots of includes this can cause countless wasted hours looking for where the variable is set before finding in the PHP manual that the interpreter did this automatically for you. Now you may argue that $_GET, $_POST… are also set automatically. However, the core difference is that these are self documenting in that the $_GET part indicates that it came from GET, likewise for all the other super globals.
No namespaces
This is something that is coming in PHP 5.3 and I am looking forward to using it. However, current PHP versions still do not have them so the following is still true at time of writing.
When a application grows beyond the simple stage, developers look for a way of organizing the code into logical structures. Now there is one particular code-base that I maintain which simply uses classes with all static methods to get around the issue.This is something that truly frustrates me from time-to-time as it creates a interaction graph which is so tightly coupled, you struggle to add features or re-factor it.
By interaction graph I mean, if you drew a diagram where each class is a circle, each method is a circle within the class circle and the calls from one class to another are arrows. This results a very dense graph where nearly all of the nodes are connected to each other. I strive for my interaction graphs to have as few connections as possible and try to keep a very simple and hierarchal when possible. This results in the code being less-coupled and more flexible.
Namespaces allow you to group functions or classes into packages which you can then load and reference. If you want to find out more, visit the php.net website.
All API functions in global scope
This one is related to the no namespaces problem. In a PHP script all of the API functions are available without any loading in all scopes.
About a year and a half ago, my employer decided to upgrade PHP from version 4 to 5 (finally!). Anyway, a new function was introduced into PHP called str_ireplace(). Because PHP 4 did not have this function, it was implemented as a custom function. When we upgraded PHP this caused fatal errors on every page of the website - great! Now while this particular example probably could have been avoided as it was inevitable that this function would be introduced into PHP at some stage, it still illustrates a problem. What if you declare a function or class which the PHP API decides to override later. Not only do you have to then rename your class/function (assuming the new built in one does not match your functionality) and then change every point at which the class or function is used.
In other languages such as Python, Java and C, functions are optionally loaded using some import mechanism. Java and python also provide namespaces on top of this so that two separate modules of functionality can have the same name without conflicting. Namespaces and optional inclusion of functions is something I would like to see.
Summary
While much of this article explains the issues I encounter with PHP and it can seem very against PHP, I have not discussed all the things I like about PHP. And for every thing I do not like, there is about five I do like. This was just a way of sharing the annoyances I face with PHP every day.
Is there anything that really annoys you? If so post a comment and let us all know.
Comments
Dave
06 Feb 2009 at 09:31
+1 on the indirection operator. It’s annoying. I end up typing -. or _.
Wesley Walser
06 Feb 2009 at 19:03
The indirection operator -
I suppose I have gotten use to typing it. ‘.’ is absolutely nicer as I call methods more often than I concat strings.
I don’t think it’s that php CAN’T do operator overloading they just choose not too. It’s a language design decision and like most things had trade offs in both directions. Languages that do operator overloading often have interesting things like var = ‘3’ + 3 and var = 3 + ‘3’ producing different results.
No type hinting for scalar types -
I can see why this is annoying.
I am guessing that the use case that they saw for this was people who want to check for objects and arrays since these are the two types that aren’t easily cast to another type. As with most loosely typed languages writing proper @var documentation for methods is the best solution if this problematic.
Register globals -
Turn register globals off, please, turn it off.
No namespaces -
I agree that adding namespaces to PHP is going to be a great step forward, but I don’t know that it solves the problem of coupled code. By putting things into classes and calling them statically you basically created namespaces so if namespaces are going to solve the problem then it seems like your solution should have worked as well.
All API functions in global scope -
While annoying it is also one of the biggest reasons that PHP seems to approachable to most beginners. This may or may not seem like a good thing to you, but being approachable has created mass adoption and mass adoption has benefited the PHP community greatly.
I agree that there are a bunch of little annoying things in PHP but I also agree with your statement that for every one of those there are enough nice things to tip the scale. The language has become a lot nicer over the past few years and now that development is rolling along again it seems like 5.3 will be bringing some very welcome new treats.
Wesley Walser
06 Feb 2009 at 19:05
Sorry about the long comment, it seems that double newlines don’t play well around here. It was split up a bit nicer when I was typing it.