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
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: 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

Pull Requests

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: Wed Dec 04 21:01:29 2024 UTC