php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71950 static variables are incorrect after call parent::method()
Submitted: 2016-04-03 18:18 UTC Modified: 2016-04-09 08:28 UTC
From: mika018 at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Irrelevant OS: Windows 10 64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mika018 at gmail dot com
New email:
PHP Version: OS:

 

 [2016-04-03 18:18 UTC] mika018 at gmail dot com
Description:
------------
When you call parent::method() and there is a static variable in this, the value returned to all inherited objects is the same

Test script:
---------------
class A
{
	function a()
	{
		static $i;
		
		if (!$i)
		{
			$i = $this->value;
		}
		
		return $i;
	}
}

class B extends A
{
	public $value = 'B';
}

class C extends A
{
	public $value = 'C';
	
	function a()
	{
		return parent::a();
	}
}

$b = new B;
echo $b->a();

$c = new C;
echo $c->a();

Expected result:
----------------
BC

Actual result:
--------------
BB

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-03 22:19 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-04-03 22:19 UTC] requinix@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

Static variables will persist their values across every call. *Every* call.
 [2016-04-09 08:28 UTC] mika018 at gmail dot com
-: michael dot bilcot at gmail dot com +: mika018 at gmail dot com
 [2016-04-09 08:28 UTC] mika018 at gmail dot com
Ok thank you

My test was distorted because of "a" method is the A class construct too.
Sorry, my bad
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 09:01:29 2024 UTC