php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53155 conditional class creation does not reset static variable in method
Submitted: 2010-10-25 21:41 UTC Modified: 2015-01-08 20:35 UTC
Votes:5
Avg. Score:3.6 ± 1.5
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: frederic dot hardy at mageekbox dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.3 OS: FreeBSD
Private report: No CVE-ID: None
 [2010-10-25 21:41 UTC] frederic dot hardy at mageekbox dot net
Description:
------------
when a class which extends a parent class with a method which define a static variable is created then a condition is verified, the static variable is not reset in the chield class.

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

abstract class singleton
{
	public static function getInstance()
	{
		static $instance = null;

		if ($instance === null)
		{
			$instance = new static();
		}

		return $instance;
	}

	protected function __construct() {}
	protected function __clone() {}
}

class a extends singleton {
}

var_dump(a::getInstance());

if (1 > 0)
{
	class b extends a {}
}

var_dump(b::getInstance());

?>

Expected result:
----------------
object(a)#1 (0) {
}
object(b)#2 (0) {
}    

Actual result:
--------------
object(a)#1 (0) {
}
object(a)#1 (0) {
}  

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-27 02:54 UTC] felipe@php.net
Probably related to bug #48623
 [2012-05-11 21:05 UTC] ash at itsash dot co dot uk
I have the same issue on 5.3.6 on Ubuntu.

static::$classVariable with late static binding causes php to override the parent 
static variable.

When using static $methodVariable = 'first_value';  if this is later changed, by 
$methodVariable, when a new instance of that class is initiated, the 
$methodVariable SHOULD be 'first_value', however UNEXPECTEDLY it carries over 
from any prior instance.
 [2012-10-26 16:14 UTC] dagguh at gmail dot com
Another proof that LSB produces bad code.
Just look at it.

There is no such thing as static inheritance, so maybe you should simply learn to 
code.
 [2015-01-08 20:35 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2015-01-08 20:35 UTC] danack@php.net
Yep, the code is behaving as it is defined in the language.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 14:01:31 2024 UTC