Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

ZendCode - allow const and property of same name #6274

Closed
Danack opened this issue May 14, 2014 · 4 comments
Closed

ZendCode - allow const and property of same name #6274

Danack opened this issue May 14, 2014 · 4 comments
Assignees
Milestone

Comments

@Danack
Copy link

Danack commented May 14, 2014

Hi,

PHP allows classes to have properties and consts of the same name, but ZendCode doesn't support this.

use Zend\Code\Reflection\ClassReflection;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\PropertyGenerator;


require_once('../vendor/autoload.php');


class OutputClass
{
    private $x = "I'm not sure why,";

    const x = ' would I ever want this?';

    function foo() {
        echo $this->x;
        echo self::x;
    }
}

$test = new OutputClass();

$test->foo();

$reflector = new ClassReflection('OutputClass');
$classGenerator = ClassGenerator::fromReflection($reflector);
$methods = $reflector->getMethods();

foreach ($methods as $method) {
    $methodGenerator = MethodGenerator::fromReflection($method);
    $classGenerator->addMethodFromGenerator($methodGenerator);
}

$constants = $reflector->getConstants();
foreach ($constants as $name => $value) {
    $classGenerator->addProperty($name, $value, PropertyGenerator::FLAG_CONSTANT);
}

$properties = $reflector->getProperties();
foreach ($properties as $property) {
    $classGenerator->addPropertyFromGenerator($property);
}

$text = $classGenerator->generate();
echo $text;

Outputs:

I'm not sure why, would I ever want this?
PHP Fatal error: Uncaught exception 'Zend\Code\Generator\Exception\InvalidArgumentException' with message 'A property by name x already exists in this class.' in /somedir/vendor/zendframework/zend-code/Zend/Code/Generator/ClassGenerator.php:475
F

@Danack
Copy link
Author

Danack commented May 15, 2014

Tested against head. The issue is still there, but the code example above doesn't work. Here is a new example that shows the issue:

use Zend\Code\Reflection\ClassReflection;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\PropertyGenerator;


require_once('./vendor/autoload.php');


class OutputClass
{
    private $x = "I'm not sure why,";

    const x = ' would I ever want this?';

    function foo() {
        echo $this->x;
        echo self::x;
    }
}

$test = new OutputClass();

$test->foo();

$reflector = new ClassReflection('OutputClass');
$classGenerator = ClassGenerator::fromReflection($reflector);


$constants = $reflector->getConstants();
foreach ($constants as $name => $value) {
    $classGenerator->addProperty($name, $value, PropertyGenerator::FLAG_CONSTANT);
}

$text = $classGenerator->generate();
echo $text;

Also note that ClassGenerator::fromReflection($reflector); isn't adding the constant.

steverhoades added a commit to steverhoades/zf2 that referenced this issue May 15, 2014
@steverhoades
Copy link
Contributor

@Danack Please see my latest branch for this issue: https://github.com/steverhoades/zf2/tree/hotfix/6274. If you could please let me know if this resolves your issue I would appreciate it.

  • Steve

@Danack
Copy link
Author

Danack commented May 16, 2014

Hi Steve,

Yep that branch works fine for this issue.

cheers
Dan

@Ocramius
Copy link
Member

Handled in #6288

freax pushed a commit to freax/zf2 that referenced this issue Nov 27, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants