Caution: The documentation you are viewing is
for an older version of Zend Framework.
You can find the documentation of the current version at:
Zend_CodeGenerator Examples - Zend_CodeGenerator
Example #1 Generating PHP classes
The following example generates an empty class with a class-level DocBlock.
The above code will result in the following:
Example #2 Generating PHP classes with class properties
Building on the previous example, we now add properties to our generated class.
The above results in the following class definition:
Example #3 Generating PHP classes with class methods
Zend_CodeGenerator_Php_Class allows you to attach methods with optional content to your classes. Methods may be attached as either arrays or concrete Zend_CodeGenerator_Php_Method instances.
The above generates the following output:
Example #4 Generating PHP files
Zend_CodeGenerator_Php_File can be used to generate the contents of a PHP file. You can include classes as well as arbitrary content body. When attaching classes, you should attach either concrete Zend_CodeGenerator_Php_Class instances or an array defining the class.
In the example below, we will assume you've defined $foo per one of the class definitions in a previous example.
Calling generate() will generate the code -- but not write it to a file. You will need to capture the contents and write them to a file yourself. As an example:
The above will generate the following file:
Example #5 Seeding PHP file code generation via reflection
You can add PHP code to an existing PHP file using the code generator. To do so, you need to first do reflection on it. The static method fromReflectedFileName() allows you to do this.
Example #6 Seeding PHP class generation via reflection
You may add code to an existing class. To do so, first use the static fromReflection() method to map the class into a generator object. From there, you may add additional properties or methods, and then regenerate the class.