Zend Framework 2.0 Roadmap Review

Being one of the most famous PHP frameworks in the world, Zend Framework has set up its roadmap for version 2.0. As ZF 1.10 may certainly be the final release of 1.x branch, we are now going to be focused on ZF 2.0.


The strikingly attractive and interesting thing is that ZF 2.0 will turn to be fully PHP 5.3 supportted, that means we can make use of many new language features such as :


1. namespaces
2. __invoke()
3. closures
4. goto
5. Late Static Bindings (LSB)
etc.


Other important features are listed below :


1. Unified constructor


This means every constructor of component will be based on “options”, for example :

$options = array('bar' => 'baz');
// to initialize object using :
$foo = new Foo($options);
// or :
$foo = new Foo();
$foo->setOptions($options);


This allows for more flexibility to add new configurations and is a good technique for allowing Dependency Injection.


2. Options


Option keys will become all_lowercase_underscore_keys

$options = array(
    'first_lowercase_underscore_key' => 'first',
    'second_lowercase_underscore_key' => 'second'
);


3. Exceptions


Now Zend_Exception becomes an interface, and its position will be replaced by custom exceptions and SPL exceptions.

interface Exception
{
    // Exception becomes interface
}
class myException extends InvalidArgumentException implements Exception
{
    // myException extends from SPL exception and implements Exception interface
}


4. Design By Contract


In one word, number of interfaces would be used for ease maintenance and testing. The dispatcher will works more faster by following standard use cases.


Basically saying, the DBC let you “always think about interface and testing before class”. See Design by contract for more details.


5. Elimination of most singletons


Zend_Controller_Front will be probably unsingled in the future.
Indiscriminate use of singletons will disappear (I hope).


Think about the truth meaning of the code listing below :

protected function __construct() {}

/**
 * Enforce singleton; disallow cloning
 *
 * @return void
 */

private function __clone() {}

/**
 * Singleton instance
 */

public static function getInstance()
{
    if (null === self::$_instance) {
        self::$_instance = new self();
    }
    return self::$_instance;
}


In fact, in most cases, we have to create a “reset” function to clean up the object for testing, Such as Zend_Controller_Front::resetInstance(). I think it’s time to think about “Do we really need it?”


6. Creation of components for general-purpose, cross-functional actions


Common APIs will be set up for those components which got duplicate code, such as :
Plugins/Helpers
Decorators
Factories
Cache

// Something like this (I guess)
interface Common_Helper_Interface
{
    public function getHelper($type);
    // ......
}


7. Usage of new language features within plugin architectures


The usage of strategy pattern for plugins will be replaced by using __invoke() and closures.
This is really a big improvement of performance.


So these code would be propably removed :

interface Zend_Application_Resource_Resource
{
    /**
     * Strategy pattern: initialize resource
     *
     * @return mixed
     */

    public function init();
}


And Zend_Application_Resource_View::init() would be propably replaced from :

class Zend_Application_Resource_View extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * Defined by Zend_Application_Resource_Resource
     *
     * @return Zend_View
     */

    public function init()
    {
        ......
        return $view;
    }
}

To :

class Zend_Application_Resource_View extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * init() is replaced by _invoke()
     *
     * @return Zend_View
     */

    public function _invoke()
    {
        ......
        return $view;
    }
}


8. Autoload-only

No require_once, clean and neat, something like in Symfony.


9. Namespaces

The first PHP framework using namespace, though it’s still not so perfect in PHP.

class Zend_Controller_Front
{
    ......
}
// would propably becomes :
namespace ZendController
class Front
{
    ......
}


10. Goto
It’s evil …

The current MVC implementation of ZF is very slow. But the team has make up their mind to solve the problem in 2.0. Techniques are designed to faster the whole MVC layer :

1. Strict interfaces

2. Explicit route

Inspired by ROR and Python Routes Project, it’s something similar to symfony’s routing.yml?
see Zend_Controller_Router 2.0

3. State machine of front controller

Zend_Controller_Front::dispatch() may be simplified by using goto :

// just imagine this
public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
{
    if (!$this->getParam('noErrorHandler')) goto A;
    ......
    if (!$this->getParam('noViewRenderer')) goto B;
    ......
    if (null !== $request) goto C;
    ......
    if (null !== $response) goto D;
    ......
    if (!$this->_request->isDispatched()) goto XXX;
    ......
}


Details in Zend_Controller 2.0
For more details about what’s new in 2.0, see here

Posted in Zend Framework | Tagged | Leave a comment