Thanks to this series I’ve made a time travel. Travel to my first experiences in PHP, which were my companion for about two and a half years.

Old, custom framework times

I remember when I was creating a web application in our own framework. It was barely object oriented, had a couple of files with over 5k lines and used pure MySQL queries to retrieve data. Classic 😉

In those times if you wanted to use some external library or even your own class – you used include or require to attach it to the current file. I did it that way for a long time. And that was a nightmare.

If you were smart you could use an autoloader. It was available since version 5.0. For me it took 2 years to discover it – I’ve used include for a very long time, so it was ok to me to continue. But with autoloader I could forget about including files, just type new SomeClass and poof, it there. Autocompleter is described later in this post. But there was another problem…

Namespaces

Classes didn’t have namespaces. If I have two with the same name – I was doomed. Zend tried to fight with that with pretty ok but ugly solution: naming class with all directories’ names included. The name for class /Foo/Bar/Baz/SomeClass.php was class Foo_Bar_Baz_SomeClass. There were many ugly, long class’ names, but it worked. Even Google used it in it’s Gmail API (Google_Service_Gmail for example).

That was hard times. But then I’ve discovered namespaces. Whoa! That was it. I could name the class very short, class SomeClass and give it namespace corresponding to its location in the project, namespace Foo\Bar\Baz; for example. And that was a killer feature, available from version 5.3.0. It was a real game changer. Classes have their own namespaces, names can be short and the problem with names collisions just disappeared.

Ok, I had namespaces, short classes’ names etc. But they have to be somehow included in the project. Where was an option to include all class files, then use them, but it would look like this:

It worked ok but has many repetitions and requires knowledge of locations for each class.

Autoloader

And here comes autoloader. It was available since PHP 5.0 so it took me some time to discover it 😉 Autoloader is a simple script that specifies directory hierarchy for namespaces and class names for their files names.

Simple autoloader for previous example might looks like this:

And that’s it! Simple script enabling easy including classes. Of course, it is an only simple example, real autoloaders are far more complicated and gives more possibilities. And are rather generated automatically, because of…

Composer

The composer is one of the best things that happened to PHP. It’s dependency manager for PHP, like npm for nodejs or bundler for ruby. If I want to use some open source library I just need to specify it in simple composer.json file. For one of my projects it looks like:

After some basic information about the project, like name and description, there are listed required packages. I can use different version conditions. Now if I want to start developing I must run simple command $ composer install and puff – all packages are downloaded and installed in the project. And because of possible different conventions within every package – the autoloader has to be generated every time dependencies changed. But for a programmer it is transparent – just install dependencies and every class is available by specifying its namespace.

Summary

Ok, that’s a lot of information, so let’s sum up.

Before version 5.3 developing in PHP sucks. Classes have to have unique names, should be explicitly included in the project and sometimes have veeeery long names (to prevent names collisions). But from version 5.3 (5.3.2 to be more specific) when the composer has been created using classes becomes… ordinary 😉 Short names, namespaces, autoloading and dependency management. That’s all that you may expect from modern language. And I can say with full responsibility – PHP is a modern language and its a pleasure to develop in it. Period.

Don’t forget to check out other PHPhyts!