ZF-11513: Support for constants and properties with the same name in a class


Atm the {{Zend_CodeGenerator_Php_Class::setProperty()}} function throws an exception if you try to add a class constant with the same name as a normal property. This behavior is not correct. PHP allows for class constants and properties to have the same name.

This is actually a valid PHP class:

class My_Class  {
    const name="some constant content";
    public $name = "some dynamic content";


The solution to this trivial problem is really just to add an open space behind the property name " ".

While that does work, it's not a robust solution. {{Zend_CodeGenerator_Php_Class}} treats class constants and properties the same internally (stored in associative array indexed by name), and so trying to add a constant and a property with the same name will cause a key collision. The class will need to be restructured to treat those two items differently. I'll work on a prototype implementation and report back.

Prototype implementation. Provides separate methods for manipulating class constants while keeping backwards-compatibility (constants can still be added via {{setProperty}}/{{setProperties}}). The only possible break is that {{getProperty}}/{{getProperties}} will no longer enumerate constants.


I've reviewed the patch and confirm it operates as intended. Recommend committing to trunk (need to check with Ralph on adding to release branch as it adds to the API). Please ensure a ZF2 git pull request including matching changes is issued for ZF2 before resolving.

Fixed in trunk r24418

Issued pull request on {{zendframework/zf2}} branch {{master}}