Introduction

Zend_CodeGenerator Examples

Example #1 Generating PHP classes

The following example generates an empty class with a class-level DocBlock.

  1. span style="color: #ff0000;">'shortDescription' => 'Sample generated class',
  2.     'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
  3.     'tags''name'        => 'version',
  4.             'description' => '$Rev:$''name'        => 'license',
  5.             'description' => 'New BSD',
  6.         ),
  7.     ),
  8. ));
  9. $foo->setName('Foo'

The above code will result in the following:

  1. /**
  2. * Sample generated class
  3. *
  4. * This is a class generated with Zend_CodeGenerator.
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. *
  9. */

Example #2 Generating PHP classes with class properties

Building on the previous example, we now add properties to our generated class.

  1. span style="color: #ff0000;">'shortDescription' => 'Sample generated class',
  2.     'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
  3.     'tags''name'        => 'version',
  4.             'description' => '$Rev:$''name'        => 'license',
  5.             'description' => 'New BSD',
  6.         ),
  7.     ),
  8. ));
  9. $foo->setName('Foo''name'         => '_bar',
  10.             'visibility'   => 'protected',
  11.             'defaultValue' => 'baz''name'         => 'baz',
  12.             'visibility'   => 'public',
  13.             'defaultValue' => 'bat''name'         => 'bat',
  14.             'const''defaultValue' => 'foobarbazbat'

The above results in the following class definition:

  1. /**
  2. * Sample generated class
  3. *
  4. * This is a class generated with Zend_CodeGenerator.
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. *
  9. */'baz''bat';
  10.  
  11.     const bat = 'foobarbazbat';
  12.  
  13. }

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.

  1. span style="color: #ff0000;">'shortDescription' => 'Sample generated class',
  2.     'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
  3.     'tags''name'        => 'version',
  4.             'description' => '$Rev:$''name'        => 'license',
  5.             'description' => 'New BSD',
  6.         ),
  7.     ),
  8. ));
  9. $foo->setName('Foo''name'         => '_bar',
  10.             'visibility'   => 'protected',
  11.             'defaultValue' => 'baz''name'         => 'baz',
  12.             'visibility'   => 'public',
  13.             'defaultValue' => 'bat''name'         => 'bat',
  14.             'const''defaultValue' => 'foobarbazbat'// Method passed as array
  15. 'name'       => 'setBar',
  16.             'parameters''name' => 'bar'),
  17.             ),
  18.             'body'       => '$this->_bar = $bar;' . "\n" . 'return $this;',
  19.             'docblock''shortDescription' => 'Set the bar property',
  20.                 'tags''paramName' => 'bar',
  21.                         'datatype'  => 'string''datatype'  => 'string',
  22.                     )),
  23.                 ),
  24.             )),
  25.         ),
  26.         // Method passed as concrete instance
  27. 'name' => 'getBar',
  28.             'body'       => 'return $this->_bar;',
  29.             'docblock''shortDescription' => 'Retrieve the bar property',
  30.                 'tags''datatype'  => 'string|null'

The above generates the following output:

  1. /**
  2. * Sample generated class
  3. *
  4. * This is a class generated with Zend_CodeGenerator.
  5. *
  6. * @version $Rev:$
  7. * @license New BSD
  8. */'baz''bat';
  9.  
  10.     const bat = 'foobarbazbat';
  11.  
  12.     /**
  13.      * Set the bar property
  14.      *
  15.      * @param string bar
  16.      * @return string
  17.      *//**
  18.      * Retrieve the bar property
  19.      *
  20.      * @return string|null
  21.      */

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.

  1. span style="color: #ff0000;">'classes''docblock''shortDescription' => 'Foo class file',
  2.         'tags''name'        => 'license',
  3.                 'description' => 'New BSD',
  4.             ),
  5.         ),
  6.     )),
  7.     'body'     => 'define(\'APPLICATION_ENV\', \'testing\');',
  8. ));

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:

  1. span style="color: #ff0000;">'Foo.php', $code);

The above will generate the following file:

  1. span style="color: #808080; font-style: italic;">/**
  2. * Foo class file
  3. *
  4. * @license New BSD
  5. */
  6.  
  7. /**
  8. * Sample generated class
  9. *
  10. * This is a class generated with Zend_CodeGenerator.
  11. *
  12. * @version $Rev:$
  13. * @license New BSD
  14. */'baz''bat';
  15.  
  16.     const bat = 'foobarbazbat';
  17.  
  18.     /**
  19.      * Set the bar property
  20.      *
  21.      * @param string bar
  22.      * @return string
  23.      *//**
  24.      * Retrieve the bar property
  25.      *
  26.      * @return string|null
  27.      */'APPLICATION_ENV', 'testing');

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.

  1. span style="color: #ff0000;">"\n\$foo->bar();"

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.

  1. span style="color: #ff0000;">'name'       => 'setBaz',
  2.     'parameters''name' => 'baz'),
  3.     ),
  4.     'body'       => '$this->_baz = $baz;' . "\n" . 'return $this;',
  5.     'docblock''shortDescription' => 'Set the baz property',
  6.         'tags''paramName' => 'baz',
  7.                 'datatype'  => 'string''datatype'  => 'string',
  8.             )),
  9.         ),
  10.     )),
  11. ));
  12. $code = $generator->generate();

Introduction