How To Do RAD Prototyping and Development With The ReflectionBasedAbstractFactory

How To Do RAD Prototyping and Development With The ReflectionBasedAbstractFactory

Rapid application development isn't normally associated with Zend Framework. That's considered Laravel's domain. But thanks to the ReflectionBasedAbstractFactory, prototyping and rapid application development is now just as easy in Zend Framework as it is in Laravel. In today's tutorial, I'm going to show you how.

When using Zend ServiceManager, it's quite common to create a factory class for any class which require constructor dependencies. While tedious, it ensures we both follow development best practices and that the code we create is fully testable.

However, if we're not careful, it can lead to an enormous amount of factories — perhaps where we have one factory for every class. Needless to say, that can seriously hurt development. There needs to be a better way.

And there is!

If you're not familiar with it, it's time to get to know the ReflectionBasedAbstractFactory. This is another new addition in the 3.2.0 release of Zend ServiceManager. As the name implies, it uses PHP's Reflection API to instantiate classes.

However, as with FactoryCreator and ConfigDumper, which we covered in the last two tutorials, it's not perfect. But, it's already an excellent start on the path to excellent rapid prototyping support in Zend Framework.

Why Would You Use It?

You have tight deadlines to hit during the development of your new application. You need to get the prototype up and running as quickly as possible. If you spend (read waste) time at this stage, being extremely diligent, creating factory classes for each one, along with all the config then you know you won't hit your deadlines.

At this stage, you need to create the core functionality; instantiation and performance aren't your top priorities. In this situation, use the ReflectionBasedAbstractFactory.

How Does It Work?

Here's How. Assuming that you're working with the class below, which takes a JournalTable object as a constructor dependency.

<?php

namespace App\Service;

use App\Entity\Journal;
use App\ServiceManager\TableGateway\JournalTable;

class JournalService implements JournalServiceInterface
{
    public function __construct(JournalTable $dataSource)
    {
    }
}

Typically, you'd create a factory instantiate it, supplying it with the JournalTable object retrieved from the ServiceManager. Instead of doing that, add the following to your ServiceManager configuration.

return [
  'abstract_factories' => [
    ReflectionBasedAbstractFactory::class,
  ],
  'factories' => [
    JournalService::class => ReflectionBasedAbstractFactory::class
  ],
]

In the abstract_factories element, we load the ReflectionBasedAbstractFactory. Then, in the factories element, we set the JournalService to be loaded by the ReflectionBasedAbstractFactory.

Assuming that your JournalTable is already registered as a service, then it will be retrieved and passed to the JournalService when it's instantiated. Thanks to the magic of reflection, it can figure the rest out for you.

It's Not For Production

This is important to remember. While it saves you a lot of work instantiating your classes, as with anything that uses PHP's Reflection API, it's going to be slow. Maybe that performance hit's OK with you - but I doubt it!

While it's an excellent choice for loading your classes in development, don't use it in production. When it comes time for production, or when the core functionality of your app is far enough along, use ConfigDumper and FactoryCreator to refactor the application to use ConfigAbstractFactory instead.

Doing so, you'll be able to get the application's core code completed quickly, yet still make it production ready. It's the perfect combination of speed and best practices What's more, it works with the MVC and Zend Expressive.

 In Conclusion

This has been a rapid run-through of the ReflectionBasedAbstractFactory, yet another addition in the Zend ServiceManager 3.2.0 release. If you want to know more, check out the online documentation.

I hope that you're starting to see the complete tooling picture which is happening in Zend Framework and that it's not just an enterprise framework, but a RAD tool as well.

Just because some people want you to think that it's all "corporate" and "serious", don't let them fool you. It's becoming just as fast and flexible as Laravel for application development! And you can quote me on that.


About Matthew

Matthew Setter Matthew Setter is a PHP & Zend Framework specialist. If you're in need of a custom software application, need to migrate an existing legacy application, or want to know your current application's GPA - get in touch.

Want To Be A Zend Framework Guru?

Drop your email in the box below, and get awesome tutorialsjust like this one — straight to your inbox, PLUS exclusive content only available by email.