Skip navigation.
Home
That which cannot be rendered in binary is by definition a delusion
 

The Return of the Return of the Line Break in Zend_Reflection

I can't tell if there is a problem in Zend_Reflection or the underlying PHP_Reflection upon which it is based, but I used reflection to create actions in ZF, that is, to add a method to the controller class definition, and got a case of odd return doublings. That is, an extra line was spaced throughout the body text of the methods that already existed.

For those unfamiliar with reflection it is a system that bridges a code file with an objective model of the same code file; much as the XML libraries create a nested set of objects for working with an XML blocks' properties, values and nodes, the reflection class digests a PHP class definition -- not an object instance, but the actual source file definition in the file system. Details are here, in the ZF documentation.

An environmental note: I am using Mac OSX/Netbeans, MAMP, the latest Zend Framework (1.9) and PHP 5.2.5.

I am also using output buffering to create the body of the new modules. the OLD modules, however, are just being read in through reflection.

When I look at them using my IDE (or echoed onto the screen) they have wierd double returns.

The fix is a bit elusive; it seems to be the last line of this:

    $zrf = new Zend_Reflection_File($controller_path); // i.e.., /home/dave/.../ApiController.php
    $crf = array_shift($zrf->getClass($controller_class_name)); // i.e., Admin_ApiController
    $class = new Zend_CodeGenerator_Php_Class($crf); // extracts the class code from the file.

    $method = new Zend_CodeGenerator_Php_Method();
    $method->setName($action_name)
           ->setBody($action_method_boy);
    $class->setMethod($method);

    $file = new Zend_CodeGenerator_Php_File();
    $file->setClass($class); // put the reflection object of the class into the file

    $php_text = $file->generate();
    $php_text_compressed = preg_replace(~[\r\n][\s]*[\r\n]~', "\r",  $file->generate());

that is, any break characters seperated by nothing more than whitespace. It took some time to diagnose, hopefully, this saves someone else a little work. This seems to me to be a fairly huge flaw in the design of reflection -- you should get from the generate() method an exact replica of your original source which seems not to be the case.

Also, in using this code, it seems like 5-10% of the time, code that is syntactically correct (runs) seems to parse badly -- function parameters vanish or get wierdly concatenated, etc. In short, while reflection often works, it is not 100% reliable and should not be used without backing up the original version in some form. By the way I do not know at this point whether it is an issue with the PHP reflection system or the Zend implementation thereof; I believe it is the former.

 

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <p> <span><small> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options