How To Do RAD Prototyping and Development With The Reflection Based Abstract Factory

How To Do RAD Prototyping and Development With The Reflection Based Abstract Factory

Rapid application development isn’t often associated with Zend Framework. However, thanks to the ReflectionBasedAbstractFactory, rapid prototyping and developing in Zend Framework is as simple as Laravel. Come find out how in this tutorial.

How To Do RAD Prototyping and Development With The ReflectionBasedAbstractFactory

Rapid application development isn’t generally associated with Zend Framework. That’s considered Laravel’s domain. However, 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. 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.


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' => [
  '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 much 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.

You might also be interested in these tutorials too...

Vim - The Distraction Free Editor
Thu, Jul 20, 2017

Vim - The Distraction Free Editor

A little while ago, I took to Twitter in a sense of jubilant excitement announcing that VIM was THE distraction-free editor. As it’s been quite some time since, I honestly don’t remember exactly what it was that motivated me to do so.

Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus