php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64645 Static methods with static variables keep their value
Submitted: 2013-04-13 20:46 UTC Modified: 2013-04-15 06:28 UTC
From: pretzlaw at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.14 OS: Ubuntu 12.10 L 3.5.0-27-generic
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pretzlaw at gmail dot com
New email:
PHP Version: OS:

 

 [2013-04-13 20:46 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-15 03:01 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2013-04-15 03:01 UTC] laruence@php.net
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;
    }
 [2013-04-15 06:28 UTC] pretzlaw at gmail dot com
douh!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 13:01:30 2025 UTC