Why PSR-0 may be one of the most significant things to happen to PHP ever

Reposted from old site – original date: Friday 17 February 2012

What is PSR-0?

Very basically, PSR-0 is a recently defined standard for autoloading classes in PHP-5.3.x and above.
If you would like to have a much closer look, a copy of the final document can be found at https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

I am not going to spend much time on explaining the document or autoloading in general, as that is an exercise best left to the reader, but as a quick introduction to those that have never worked with this kind of thing before, I will explain the very basics and leave it at that.

Basically, in the old days, you would have a class with name, say, User.php to do something with users. Great. Now to use that code you needed to go through certain steps in order to include() it and then instantiate and use it, like so:

require “User.php”;
$user = new User(“Paul”);

So far, so good, right? Right! OK, so skip forward a few years and suddenly we see that there is a magic function called __autoload() in PHP5. Awesome! Let’s look at how that works then:

public function __autoload($className) {
$filename = $className . “.php”;
if (is_readable($filename)) {
require $filename;
}
}

OK, nice. Now we can load up files with a specific file naming structure with relative ease. One very big drawback here is that you could only ever register a single autoloader, which, in many cases was OK, but not the best situation. Skip forward to PHP-5.1.x and we see spl_autoload() being introduced. What this allowed was multiple autoloaders with the spl_autoload_register() function. Cool, now we could have a function to autoload the different components of our frameworks (such as controllers, views and models) with slightly different autoload functions, which tends to increase the security slightly as well as making tracking down bugs a lot easier.

This was all great stuff, and made our lives as large project developers much easier, but we soon started to see some cracks in the glossy sheen of autoloading. What people started to notice very quickly is that everyone was doing it their way. Look at a few examples of class names from projects that I have worked on:

1. Chisimba – user_class_inc.php
2. PEAR – PEAR_User which translates to PEAR/User.php
3. Zend – Zend_Cache which translates to Zend/Cache.php
4. etc. (see class.name.php, class_name.php, class.name.inc.php, classname.inc.php, etc…)

This is part of the reason that PSR-0 was conceived; to try and standardise some of this mess.

What PSR-0 tries to do is to make sure that all classes and reusable objects are kept as such, reusable, across many different frameworks and codebases with minimal effort. These standards are also exploring other aspects of large project codebases such as cache interfaces as well, but those are still in active discussion.

By now, I guess you have read through the PSR-0 standard document as above. Let us take a look at it quickly.

Basically, the way that it all works is that you have to stick to a certain naming convention. That naming convention should implement a certain directory and code file layout according to a fully qualified namespace in your code. PSR-0 compliant autoloaders will then automagically be able to find the files by traversing the directory structure and include them for use in your code. Magic! An example can be found at https://gist.github.com/221634 with a SplClassLoader implementation that sticks to the standard.

In my own project(s), I have adopted the Symfony autoloader component though. You can find a bunch of information, as well as a guide and download link at http://symfony.com/doc/2.0/components/class_loader.html Please go and take a look there for additional information.

So why is this significant?

The actual subject of this post is the significance of PSR-0. Now that we have a pretty good idea of how it works, we can now start thinking of the possibilities associated with it!

Imagine starting a new project which requires a forum, a wiki and a file upload area. OK, not a huge ask, but will take time. These are also not uncommen web applications, so there will be a lot of reusable code lying around in other projects that we can use, but each class may need to be manually edited to work with the overall project, and many other finicky little subtleties may need to be addressed, which could end up taking as long as if you were to have coded everything almost from scratch. Not good.

If, however, we used a bunch of high quality components from any number of fine projects and frameworks out there already, we could simply start adding some, or all, of these components to our own project and simply writing a bit of glue code to hold them all together, which would significantly reduce the dev time on any project. Bug tracking and fixing becomes easier due to the use of namespacing and the overall velocity of the project increases. All good things.

The more projects and people that stick to these standards, (as they become available) the better. The more simple, reusable components that are created and made available via systems like packagist http://packagist.org/ the better. Many projects can now make use of these components by simply adding a line of JSON or two to a file and using a package manager like Composer (http://getcomposer.org/composer.phar) to work with them! Side note: If you do use Composer, it will automatically generate an autoload.php file for you, making this even easier!

As a final note, from a performance perspective, we all (should) know that require() and include() etc are pretty heavy operations, but by using SPL, we should even get a bit of a bonus performance boost (on top of the performance boost you get from using PHP-5.3.x and above anyway), as well as making your code more readable and maintainable. What reason could you have for not using it? :)

In conclusion, PSR-0 autoloading is the first of many standards. If you would like to participate in creating these standards, the working group is an open one and your feedback is welcomed.

The fact that these things are being collaboratively created makes for a very exciting time to be a PHP developer! This is one of the most significant things to happen to PHP since PHP5!

Keep an eye out and most importantly, have fun!

2 thoughts on “Why PSR-0 may be one of the most significant things to happen to PHP ever

  1. www

    Hi, I do believe this is a great web site. I stumbledupon it ;) I will
    revisit once again since i have saved as a favorite it.
    Money and freedom is the best way to change, may you be rich and continue to guide other people.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>