Pragmatism in the real world

Overriding module configuration in ZF2

Let's say that you install the ZF-Common's User module. By default, it sets up its routes under the /user path segment like this: vendor/ZfcUser/config/module.config.php return array( // lots of config stuff here /** * Routes */ 'ZendMvcRouterRouteStack' => array( 'parameters' => array( 'routes' => array( 'zfcuser' => array( 'type' => 'Literal', 'priority' => 1000, 'options' => array( 'route' => '/user', 'defaults' => array( 'controller' => 'zfcuser', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes'… continue reading.

Shahar Evron: Generating ZF Autoloader Classmaps with Phing

Shahar has posted a new article about how to generate ZF2 autoloader classmaps with phing. As the ZF2 classmap autoloader is quicker than the standard autoloader, this can be a benefit in production, so being able to automate the creation is beneficial. Fortunately, using ZF2′s autoloader stack and Phing, we can enjoy both worlds: while in development, standard PSR-0 autoloading is used and the developer can work smoothly without worrying about updating class maps. As… continue reading.

What problem does dependency injection solve?

Zend Framework 2 comes with a dependency injection container (DIC), as does Symfony 2 and Aura, along with many other PHP frameworks that target PHP 5.3 or higher nowadays. This article attempts to explore the problem that a DIC tries to solve. Consider this simple (contrived!) example of an Album object that has an Artist object: class Album { protected $artist; public function getArtistName() { return $artist->getName(); } } The question is how to we… continue reading.

A primer on PHP namespaces

I know that there are a lot of posts now about namespaces in PHP 5.3. This is mine which is how I learnt how they work. What are namespaces? From the PHP manual: namespaces are a way of encapsulating items Hardly the most useful of definitions, but it's a starting point! A namespace is a way of grouping code that exists across multiple files without having a naming collision. That is, you can have the… continue reading.

Using Zend\Loader\Autoloader

Autoloading is the process in PHP whereby the system attempts to load a class when it is first encountered (via new or via class_exists) if it hasn't already been loaded via a require or include. Autoload works by looking for a method called __autoload or walking through any method registered with spl_autoload_register. Zend Framework 2 provides the Zend\Loader\Autoloader component for autoloading of Zend Framework and your own classes. What does it provide? The ZF2 autoloader… continue reading.

One-to-many joins with Zend_Db_Table_Select

Let's say that you want to set up a one-to-many relationship between two tables: Artists and Albums because you've refactored my ZF1 tutorial. Let's assume that an artist has many albums. These are the basic table definitions: artists table: id, artist albums table: id, artist_id, title When you list the albums, you obviously want to see the artist name rather than the id, so clearly you use a join! Assuming you're using Zend_Db_Table, the easiest… continue reading.

Two new Sublime Text 2 packages for PHP

Stuart Herbert has written two new Sublime Text packages for PHP: Additional PHP Snippets PHPUnit The best way to install these is to install Package Control first and then use shift+cmd+P -> install package. Even better, Stuart has rolled my getter/setter creation snippet into Additional PHP Snippets, so you can now have it without any hassle! Update: Also, Ben Selby has created a package for DocBlox!

Jason Grimes: Using Doctrine 2 in Zend Framework 2

Jason Grimes has posted an article showing how to use Doctrine 2 with Zend Framework 2. He uses my tutorial as the starting point which enables him to concentrate on the Doctrine integration rather than the irrelevant details about setting a ZF2 application which is excellent. He walks through 6 steps in order to do the integration: This article shows how to set up and use Doctrine 2 in Zend Framework 2, by extending Rob’s… continue reading.

Sublime Text 2 Snippet for PHP getter and setter generation

Update: This article has been superseded by Improved Sublime Text 2 PHP getter and setter generation I've been playing with Sublime Text 2 recently and have quite enjoyed how quiet my ageing laptop is when the fans aren't running due to a Java-based IDE. As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. I needed to create a few getXxx() and setXxx()… continue reading.