1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.codegenerator.examples">
4 <title>Zend_CodeGenerator Examples</title>
6 <example id="zend.codegenerator.examples.class">
7 <title>Generating PHP classes</title>
10 The following example generates an empty class with a class-level
14 <programlisting language="php"><![CDATA[
15 $foo = new Zend_CodeGenerator_Php_Class();
16 $docblock = new Zend_CodeGenerator_Php_Docblock(array(
17 'shortDescription' => 'Sample generated class',
18 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
22 'description' => '$Rev:$',
26 'description' => 'New BSD',
31 ->setDocblock($docblock);
32 echo $foo->generate();
36 The above code will result in the following:
39 <programlisting language="php"><![CDATA[
41 * Sample generated class
43 * This is a class generated with Zend_CodeGenerator.
56 <example id="zend.codegenerator.examples.class-properties">
57 <title>Generating PHP classes with class properties</title>
60 Building on the previous example, we now add properties to our
64 <programlisting language="php"><![CDATA[
65 $foo = new Zend_CodeGenerator_Php_Class();
66 $docblock = new Zend_CodeGenerator_Php_Docblock(array(
67 'shortDescription' => 'Sample generated class',
68 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
72 'description' => '$Rev:$',
76 'description' => 'New BSD',
81 ->setDocblock($docblock)
82 ->setProperties(array(
85 'visibility' => 'protected',
86 'defaultValue' => 'baz',
90 'visibility' => 'public',
91 'defaultValue' => 'bat',
96 'defaultValue' => 'foobarbazbat',
99 echo $foo->generate();
103 The above results in the following class definition:
106 <programlisting language="php"><![CDATA[
108 * Sample generated class
110 * This is a class generated with Zend_CodeGenerator.
119 protected $_bar = 'baz';
123 const bat = 'foobarbazbat';
129 <example id="zend.codegenerator.examples.class-methods">
130 <title>Generating PHP classes with class methods</title>
133 <classname>Zend_CodeGenerator_Php_Class</classname> allows you to attach
134 methods with optional content to your classes. Methods may be
135 attached as either arrays or concrete
136 <classname>Zend_CodeGenerator_Php_Method</classname> instances.
139 <programlisting language="php"><![CDATA[
140 $foo = new Zend_CodeGenerator_Php_Class();
141 $docblock = new Zend_CodeGenerator_Php_Docblock(array(
142 'shortDescription' => 'Sample generated class',
143 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
147 'description' => '$Rev:$',
151 'description' => 'New BSD',
156 ->setDocblock($docblock)
157 ->setProperties(array(
160 'visibility' => 'protected',
161 'defaultValue' => 'baz',
165 'visibility' => 'public',
166 'defaultValue' => 'bat',
171 'defaultValue' => 'foobarbazbat',
175 // Method passed as array
178 'parameters' => array(
179 array('name' => 'bar'),
181 'body' => '$this->_bar = $bar;' . "\n" . 'return $this;',
182 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
183 'shortDescription' => 'Set the bar property',
185 new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
186 'paramName' => 'bar',
187 'datatype' => 'string'
189 new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
190 'datatype' => 'string',
195 // Method passed as concrete instance
196 new Zend_CodeGenerator_Php_Method(array(
198 'body' => 'return $this->_bar;',
199 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
200 'shortDescription' => 'Retrieve the bar property',
202 new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
203 'datatype' => 'string|null',
210 echo $foo->generate();
214 The above generates the following output:
217 <programlisting language="php"><![CDATA[
219 * Sample generated class
221 * This is a class generated with Zend_CodeGenerator.
229 protected $_bar = 'baz';
233 const bat = 'foobarbazbat';
236 * Set the bar property
241 public function setBar($bar)
248 * Retrieve the bar property
250 * @return string|null
252 public function getBar()
261 <example id="zend.codegenerator.examples.file">
262 <title>Generating PHP files</title>
265 <classname>Zend_CodeGenerator_Php_File</classname> can be used to generate the
266 contents of a <acronym>PHP</acronym> file. You can include classes as well as arbitrary
267 content body. When attaching classes, you should attach either
268 concrete <classname>Zend_CodeGenerator_Php_Class</classname> instances or an
269 array defining the class.
273 In the example below, we will assume you've defined
274 <varname>$foo</varname> per one of the class definitions in a previous
278 <programlisting language="php"><![CDATA[
279 $file = new Zend_CodeGenerator_Php_File(array(
280 'classes' => array($foo);
281 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
282 'shortDescription' => 'Foo class file',
286 'description' => 'New BSD',
290 'body' => 'define(\'APPLICATION_ENV\', \'testing\');',
295 Calling <methodname>generate()</methodname> will generate the code -- but not
296 write it to a file. You will need to capture the contents and write
297 them to a file yourself. As an example:
300 <programlisting language="php"><![CDATA[
301 $code = $file->generate();
302 file_put_contents('Foo.php', $code);
306 The above will generate the following file:
309 <programlisting language="php"><![CDATA[
318 * Sample generated class
320 * This is a class generated with Zend_CodeGenerator.
328 protected $_bar = 'baz';
332 const bat = 'foobarbazbat';
335 * Set the bar property
340 public function setBar($bar)
347 * Retrieve the bar property
349 * @return string|null
351 public function getBar()
358 define('APPLICATION_ENV', 'testing');
362 <example id="zend.codegenerator.examples.reflection-file">
363 <title>Seeding PHP file code generation via reflection</title>
366 You can add <acronym>PHP</acronym> code to an existing <acronym>PHP</acronym> file
367 using the code generator. To do so, you need to first do reflection on it. The
368 static method <methodname>fromReflectedFileName()</methodname> allows you to do
372 <programlisting language="php"><![CDATA[
373 $generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
374 $body = $generator->getBody();
375 $body .= "\n\$foo->bar();";
376 file_put_contents($path, $generator->generate());
380 <example id="zend.codegenerator.examples.reflection-class">
381 <title>Seeding PHP class generation via reflection</title>
384 You may add code to an existing class. To do so, first use the
385 static <methodname>fromReflection()</methodname> method to map the class into a
386 generator object. From there, you may add additional properties or
387 methods, and then regenerate the class.
390 <programlisting language="php"><![CDATA[
391 $generator = Zend_CodeGenerator_Php_Class::fromReflection(
392 new Zend_Reflection_Class($class)
394 $generator->setMethod(array(
396 'parameters' => array(
397 array('name' => 'baz'),
399 'body' => '$this->_baz = $baz;' . "\n" . 'return $this;',
400 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
401 'shortDescription' => 'Set the baz property',
403 new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
404 'paramName' => 'baz',
405 'datatype' => 'string'
407 new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
408 'datatype' => 'string',
413 $code = $generator->generate();