Req #63454 [Opn]: Inheriting static function with static variable defined in a trait.

From: Date: Wed, 07 Nov 2012 14:32:24 +0000
Subject: Req #63454 [Opn]: Inheriting static function with static variable defined in a trait.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-174555@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63454&edit=1

 ID:                 63454
 Updated by:         laruence@php.net
 Reported by:        zetruger at gmail dot com
 Summary:            Inheriting static function with static variable
                     defined in a trait.
 Status:             Open
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   Xubuntu 12.04
 PHP Version:        5.4.8
 Block user comment: N
 Private report:     N

 New Comment:

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"


Previous Comments:
------------------------------------------------------------------------
[2012-11-07 13:19:03] 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"


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63454&edit=1


Thread (8 messages)

« previous php.bugs (#174555) next »