PHP Framework Interop Group

Today the “PHP Framework Interop Group” got it’s brand new website that was designed to serve as an entry point for all those people looking for information about the newly...

Today the “PHP Framework Interop Group” got it’s brand new website that was designed to serve as an entry point for all those people looking for information about the newly born initiative of PSR. It was not clearly stated what PSR stands for (probably PHP Standard Recommendation), but it’s clear what it’s trying to achieve.

Current status

Currently, there are 3 accepted documents called:

  • PSR-0 – covering auto-loading
  • PSR-1 – covers basic coding standard
  • PSR-2 – covers general coding standard

All of them are hosted on GitHub and can be forked by the contributors. Speaking of them, there’s a list of people involved in different community important projects which can be browsed on the¬†PHP Framework Interop Group website.

Autoloading standard

I consider it a great idea to standardize the auto-loading feature provided by PHP. Managing all the class dependencies using includes and requires was always painful and required lots of work. Then one day the autololoading feature has been released.

Since there were no conventions regarding its usage, different developers wrote their own autoloader implementations. While developing a larger project, not using external libraries provided by others is a waste of time and money – common business approach. As a result one has to integrate several autoloading functions backing up one another in order to avoid a PHP Fatal Error. The more autoloaders you had to integrate, the bigger was their impact on the system performance.

Standardizing the autoloading feature and using one mechanism can reduce the performance impact and make all libraries inter-portable between different solutions, what goes along with the code reuse spirit.

The PHP-FIG group introduced a class named SplClassLoader, that follows the PSR-0 regulations.

Perhaps one day such auto-loading feature will be embedded into PHP’s core?

Coding standards

“War… War never changes…”

Those are the words that perfectly describe my thoughts about coding standards. The topic itself is deep as the ocean, and wide as the whole wold. In the past some people related to Zend Framework or PEAR developed their own, successful coding standards that were spread around the world. Both standards were widely used and event integrated in tools such as PHP Sniffer which are checking if the created code follows the terms defined in the standard.

Defining syntax standards is always a mess. Let’s look at some examples. First the (IMHO) messy one – evil, condensed, no line width limit:

class Bar {
    public function baz($paramA,$paramB,$paramC,$paramD){
        if($paramA==$paramB&&is_array($paramC)&&isset($paramC[$paramA])&&$paramD->callingMessyMonster(null,true,false)){
            return $paramC[$paramA]*$paramB%$paramA;
        }elseif(is_array($paramC[$paramB])){
            return $paramC[$paramB]/$paramA;
        }
    }
    
    public function getConfig(){
        return array('some'=>false,'config'=>123,'valuesToSet'=>'PowerRanger');
    }
}

Evil. Messy. COMMON.

Now lets try opening bracers for classes and methods on new line but not for the controll structures like if or switch spaces after commas, and before and after separators thus partialy PSR-2 compliant.

class Bar
{
    public function baz($paramA, $paramB, $paramC, $paramD)
    {
        if ($paramA == $paramB && is_array($paramC) && isset($paramC[$paramA]) && $paramD->callingMessyMonster(null, true, false)) {
            return $paramC[$paramA] * $paramB % $paramA;
        } else if (is_array($paramC[$paramB])) {
            return $paramC[$paramB] / $paramA;
        }
    }
    
    public function getConfig()
    {
        return array('some' => false, 'config' => 123, 'valuesToSet' => 'PowerRanger');
    }
}

It still looks messy, and IMHO we should be consequent about the bracer policy and use it everywhere. The problem is that being consequent produces more lines of code, which contain only one bracer. That would be a issue when a programmer is being paid for each line of code. But for the sake of readability…

class Bar
{
    public function baz($paramA, $paramB, $paramC, $paramD)
    {
        if ($paramA == $paramB && is_array($paramC) && isset($paramC[$paramA]) && $paramD->callingMessyMonster(null, true, false))
        {
            return $paramC[$paramA] * $paramB % $paramA;
        }
        else if (is_array($paramC[$paramB]))
        {
            return $paramC[$paramB] / $paramA;
        }
    }
    
    public function getConfig()
    {
        return array('some' => false, 'config' => 123, 'valuesToSet' => 'PowerRanger');
    }
}

Long parameter, or logic check could be moved out of the if statement inot a variable to save some space. But it could be also aligned.

class Bar
{
    public function baz($paramA, $paramB, $paramC, $paramD)
    {
        if ($paramA == $paramB
            && is_array($paramC)
            && isset($paramC[$paramA])
            && $paramD->callingMessyMonster(null, true, false)
        )
        {
            return $paramC[$paramA] * $paramB % $paramA;
        }
        else if (is_array($paramC[$paramB]))
        {
            return $paramC[$paramB] / $paramA;
        }
    }
    
    public function getConfig()
    {
        return array(
            'some' => false,
            'config' => 123,
            'valuesToSet' => 'PowerRanger'
        );
    }
}

One could also set some alignment rules for the array initializer and PHPDoc comments or even a special comment style method body separation line.

/**
 * Example sensless class
 *
 * @since 1.0.0
 * @version 1.0.0
 * @author Grzegorz Godlewski <mail@me.now>
 * @license GPL
 */
 
class Bar
{

//-----------------------------------------------------------------------------

    /**
     * Does some strange mathematics
     *
     * @param int        $paramA Input variable #1
     * @param int        $paramB Input variable #2
     * @param int[]      $paramC Input variable #3 being an array
     * @param SomeObject $paramD Input variable #4 being an mysterious object
     *
     * @return int The strange mathematics result
     */
     
    public function baz($paramA, $paramB, $paramC, $paramD)
    {
        if ($paramA == $paramB
            && is_array($paramC)
            && isset($paramC[$paramA])
            && $paramD->callingMessyMonster(null, true, false)
        )
        {
            return $paramC[$paramA] * $paramB % $paramA;
        }
        else if (is_array($paramC[$paramB]))
        {
            return $paramC[$paramB] / $paramA;
        }
    }
    
//-----------------------------------------------------------------------------

    /**
     * Returns the class configuration
     *
     * @return mixed[] Configuration array
     */

    public function getConfig()
    {
        return array(
            'some'        => false,
            'config'      => 123,
            'valuesToSet' => 'PowerRanger'
        );
    }
    
//-----------------------------------------------------------------------------

} // Bar

Actually I think this kind of standard is required in a given scope and time. If you’ve ever had the possibility or was forced to work with someone else’s code, you remember that feeling where you had to get used to the coding style. You’ve probably also noticed that reading and understanding the code takes much more time – if it would be your own code that has to be reviewed after several months. That’s the place where coding standards are helpful.

With a given set of rules describing the coding standard (in terms of syntax, bracers policy etc) a developer can produce code that will be much easier to maintain not only for himself after a time, but also for other developers. That’s important both in commercial and opensource worlds (OS especially). Moreover, defining standards based on good practices can also improve the code quality.

The problem with coding standards is that they change along with the language itself. PHP was previously a procedural language, then the objects / namespaces / closures came up. Now we have traits, dereferences and shortened array syntax. Thus following a coding standard and bragging about being PSR-1 / PSR-2 compliant is somewhat vain, since after several months it could mean nothing. That’s what I meant by scope and time.

Another problem with coding standards is the scope of regulations. They usually cover syntax policy, but it also can contain code structure regulations (placing classes, their behavior [PSR-1 p. 2.3] etc). I don’t believe someone’s going to embed design pattern usage in a standard but who knows…

A small contribution

I’ve forked the SplClassLoader gist and made few tweaks for the setters methods (now allowing method chaining). You can get it from here: https://gist.github.com/3007186.

PHP 5.4 usage snippet:

require_once 'lib/SplAutoloader.php';

(new SplClassLoader(
    null,
    realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib'))
)->register();

Cheers!

About Grzegorz Godlewski

Senior Software Developer - Graduate of the Wroclaw University of Technology in Poland (Msc. Eng.). Currently working for SMT Software as a PHP / C# Backend Developer