Edit report at https://bugs.php.net/bug.php?id=64645&edit=1

 ID:                 64645
 Updated by:         larue...@php.net
 Reported by:        pretzlaw at gmail dot com
 Summary:            Static methods with static variables keep their
                     value
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Ubuntu 12.10 L 3.5.0-27-generic
 PHP Version:        5.4.14
 Block user comment: N
 Private report:     N

 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

I don't think there is a bug here.

static::self only choose the first leave static method..

try to understant the diff by changing the getInstance to:

   public static function getInstance()
    {
        static $getInstance;

        if (null == $getInstance) $getInstance = static::makeInstance();

        return $getInstance;
    }


Previous Comments:
------------------------------------------------------------------------
[2013-04-13 20:46:45] pretzlaw at gmail dot com

Description:
------------
Inherit a class with a static method. The static method has static variable in 
it. Now execute both methods and watch the variable. It wont change from one 
class to another.

Test script:
---------------
<?php

class A
{
    function __construct()
    {
        echo get_class($this->getInstance()), PHP_EOL,
                get_class(static::getInstance()), PHP_EOL,
                get_class(self::getInstance()), PHP_EOL;
    }

    public static function getInstance()
    {
        static $getInstance;

        if (null == $getInstance) $getInstance = self::makeInstance();

        return $getInstance;
    }

    public static function makeInstance()
    {
        return new stdClass();
    }
}

$a = new A();

class B extends A
{
    function __construct()
    {
        echo get_class($this->getInstance()), PHP_EOL,
                get_class(static::getInstance()), PHP_EOL,
                get_class(self::getInstance()), PHP_EOL;
    }

    public static function makeInstance()
    {
        return new ArrayObject();
    }
}

$a = new B();

Expected result:
----------------
stdClass
stdClass
stdClass
ArrayObject
ArrayObject
stdClass

Actual result:
--------------
stdClass
stdClass
stdClass
stdClass
stdClass
stdClass


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64645&edit=1

Reply via email to