PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Code Generation with Zend\CodeGenerator

Note: This article was originally published at phly, boy, phly on 26 March 2011.

Zend Framework has offerred a code generation component since version 1.8, when we started shipping Zend_Tool. Zend_CodeGenerator largely mimics PHP's ReflectionAPI, but does the opposite: it instead generates code.

Why might you want to generate code?

  • You can use it as an assistive form of "copy and paste" for common tasks (as an example, it's used in zf.sh to generate controller classes and action methods).
  • You might want to generate code from configuration, to remove the "compile" phase of generating objects from configuration values. This is often done to improve performance in situations that rely heavily on configurable values.

Zend\CodeGenerator in the ZF2 repository is largely ported from Zend Framework 1, but also includes some functionality surrounding namespace usage and imports. I used it this week when working on some prototypes, and found it useful enough that I want to share some of what I've learned.