php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63454 Inheriting static function with static variable defined in a trait.
Submitted: 2012-11-07 13:19 UTC Modified: 2012-11-07 14:32 UTC
From: zetruger at gmail dot com Assigned:
Status: Open Package: Class/Object related
PHP Version: 5.4.8 OS: Xubuntu 12.04
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-11-07 13:19 UTC] zetruger at gmail dot com
Description:
------------
There is a difference between inheriting static function with static variable 
defined in a trait of a base class and the same function defined in a base class 
directly.

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

trait Foo {
	static public function theSameFunc() {
		static $var = NULL;
		if (is_null($var)) $var = get_called_class();
		return $var;
	}
}

class A {
	use Foo;
	//static public function theSameFunc() {
	//	static $var = NULL;
	//	if (is_null($var)) $var = get_called_class();
	//	return $var;
	//}
}
class B extends A {}
class C extends B {}

var_dump(A::theSameFunc());
var_dump(B::theSameFunc());
var_dump(C::theSameFunc());

class D extends C {}
class E extends D {}
class F extends E {}

var_dump(D::theSameFunc());
var_dump(E::theSameFunc());
var_dump(F::theSameFunc());

?>

Expected result:
----------------
string(1) "A"
string(1) "B"
string(1) "C"
string(1) "D"
string(1) "E"
string(1) "F"

Actual result:
--------------
string(1) "A"
string(1) "B"
string(1) "C"
string(1) "C"
string(1) "C"
string(1) "C"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-07 14:32 UTC] laruence@php.net
it's not about triat, it's about run-time or compile-time.

the following script behavior the same as your test script:

<?php
function __autoload($ce) {
    eval(<<<'PHP'
class A {
    static public function theSameFunc() {
      static $var = NULL;
      if (is_null($var)) $var = get_called_class();
      return $var;
    }
}
PHP
);
}
var_dump(A::theSameFunc());
class B extends A {}
var_dump(B::theSameFunc());
?>

output:
string(1) "A"
string(1) "A"
 [2012-11-07 14:46 UTC] zetruger at gmail dot com
I suppose it's a bug isn't it?
Or is it normal behavior?
 [2012-11-07 19:19 UTC] mail+php at requinix dot net
Seems like a bug: the order of class declarations and var_dumps matters. With

class A { ... }
var_dump(A::theSameFunc());
class B extends A {}
class C extends B {}
class D extends C {}
class E extends D {}
class F extends E {}
var_dump(B::theSameFunc());
var_dump(C::theSameFunc());
var_dump(D::theSameFunc());
var_dump(E::theSameFunc());
var_dump(F::theSameFunc());

I get AAAAAA, but with

class A { ... }
class B extends A {}
class C extends B {}
class D extends C {}
var_dump(A::theSameFunc());
var_dump(B::theSameFunc());
var_dump(C::theSameFunc());
var_dump(D::theSameFunc());
class E extends D {}
class F extends E {}
var_dump(E::theSameFunc());
var_dump(F::theSameFunc());

I get ABCDDD.
 [2012-11-07 20:16 UTC] zetruger at gmail dot com
I think static variables must be set to default value or nulled automaticly in 
the moment of defining their classes. It does not work with traits.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Feb 17 01:01:25 2019 UTC