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

Issue Type: Bug Created: 2011-06-29T07:51:38.000+0000 Last Updated: 2012-03-21T00:11:02.000+0000 Status: Closed Fix version(s): - 1.12.0 (27/Aug/12)

Reporter: Michael Johansen (michael.johansen) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_CodeGenerator

  • state:patch-ready-for-review
  • zf-caretaker-adamlundrigan
  • zf-crteam-review

Related issues: Attachments: - ZF-11513.patch


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:

<pre class="highlight"> 
class My_Class  {
    const name="some constant content";
    public $name = "some dynamic content";


Posted by Michael Johansen (michael.johansen) on 2011-06-29T08:00:18.000+0000

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

Posted by Adam Lundrigan (adamlundrigan) on 2011-07-29T01:07:26.000+0000

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.

Posted by Adam Lundrigan (adamlundrigan) on 2011-07-29T01:39:57.000+0000

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.


Posted by Pádraic Brady (padraic) on 2011-08-28T13:41:40.000+0000

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.

Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T15:23:28.000+0000

Fixed in trunk r24418

Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T19:55:59.000+0000

Issued pull request on zendframework/zf2 branch master

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.