Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug PHP Late Static Binding on 5.3.2 #13

Closed
andistryz opened this issue Nov 20, 2012 · 3 comments
Closed

Bug PHP Late Static Binding on 5.3.2 #13

andistryz opened this issue Nov 20, 2012 · 3 comments

Comments

@andistryz
Copy link

Hey,

I always get an PHP Fatal error on my cloudhoster (php 5.3.2) environment (@see https://phpinfo.cloudcontrolled.com/)

PHP Fatal error: Cannot access self:: when no class scope is active in /srv/www/library/vendor/aws/aws-sdk-php/src/Aws/Common/Enum.php on line 50.

After changing code in \Aws\Common\Enum.php from

public static function values()
{
    $class = get_called_class();
    if (!isset(self::$cache[$class])) {
        $reflected = new \ReflectionClass($class);
        self::$cache[$class] = $reflected->getConstants();
    }

    return self::$cache[$class];
}

to (Bugfix)

public static function values()
{
    $class = get_called_class();
    if (!isset(static::$cache[$class])) {
        $reflected = new \ReflectionClass($class);
        static::$cache[$class] = $reflected->getConstants();
    }

    return static::$cache[$class];
}

it also works on PHP 5.3.2

More information at http://php.net/manual/en/language.oop5.late-static-bindings.php

@skyzyx
Copy link
Contributor

skyzyx commented Nov 20, 2012

Could you submit a pull request and any relevant unit tests for this?

@jeremeamia
Copy link
Contributor

Instead of self or static, does using Enum work?

public static function values()
{
    $class = get_called_class();
    if (!isset(Enum::$cache[$class])) {
        $reflected = new \ReflectionClass($class);
        Enum::$cache[$class] = $reflected->getConstants();
    }

    return Enum::$cache[$class];
}

That was the intention of using self.

@andistryz
Copy link
Author

public static function values()
{
    $class = get_called_class();
    if (!isset(Enum::$cache[$class])) {
        $reflected = new \ReflectionClass($class);
        Enum::$cache[$class] = $reflected->getConstants();
    }

    return static::$cache[$class];
}

Do not work.

I think this bug report https://bugs.php.net/bug.php?id=53915 (fixed in 5.3.6) should characterize the error. The bug should occur up to php version 5.3.6, too. Since \Aws\Common\Enum.php is an abstract class and every of the listed subclasses (http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.Common.Enum.html) inherid from this class the error should effect every where self::[constant] is used.

Like in

namespace Aws\DynamoDb\Enum;

use Aws\Common\Enum;

class Type extends Enum
{
    const S  = 'S';
    const N  = 'N';
    const B  = 'B';

    const SS = 'SS';
    const NS = 'NS';
    const BS = 'BS';

    const STRING     = self::S;
    const NUMBER     = self::N;
    const BINARY     = self::B;

    const STRING_SET = self::SS;
    const NUMBER_SET = self::NS;
    const BINARY_SET = self::BS;
}

I use this call to cause the error

                Zend_Debug::dump(\Aws\DynamoDb\Enum\Type::values());
                $test = array(
                        'Id' => '10101',
                        'test' => 10101,
                );
                Zend_Debug::dump(\Aws\DynamoDb\Model\Item::fromArray($test));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants