Bug #67385 [Nab]: Static property value on trait copied to object if set before class definition.
| From: | arjen at react dot com | Date: | Fri, 06 Jun 2014 07:46:59 +0000 |
| Subject: | Bug #67385 [Nab]: Static property value on trait copied to object if set before class definition. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186078@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67385&edit=1
ID: 67385
User updated by: arjen at react dot com
Reported by: arjen at react dot com
Summary: Static property value on trait copied to object if
set before class definition.
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Linux
PHP Version: 5.5.13
Block user comment: N
Private report: N
New Comment:
Description:
Static property value on trait copied to object if set before class definition.
Test script:
http://3v4l.org/nP4Pj
or
<?php
trait A {
protected static $_a;
public static function setA($a)
{
static::$_a = $a;
}
public static function getA()
{
return static::$_a;
}
}
class B {
use A;
public function __construct()
{
var_dump(static::$_a);
}
}
A::setA('AAAA');
class C {
use A;
public function __construct()
{
var_dump(static::$_a);
}
}
class D extends B {}
new B;
new C;
new D;
Expected result:
NULL
NULL
NULL
Actual result:
NULL
string(4) "AAAA"
NULL
Exactly, traits are not inheritance, so C should not inherit the static properties of trait A, just
like B doesn't.
One would expect traits to be 'mixed' into a class at compile time, not runtime so the
order of definition shouldn't matter (from https://wiki.php.net/rfc/horizontalreuse#traits_composed_from_traits)
Consider this test script http://3v4l.org/Oqrgt (slightly
modified from https://wiki.php.net/rfc/horizontalreuse#static_variables)
Counter::inc is now a static method, called once before C1 and C2 are defined.
Both C1 and C2 'inherit' the incremented value from the Counter trait.
This clashes with "The flattening property implies that all methods are independent from each
other at their usage point, even so, they might origin from the same method/trait." from the
trait RFC.
Looks like this is related to https://bugs.php.net/bug.php?id=63454 where you
commented "Seems like a bug: the order of class declarations and var_dumps matters."
Previous Comments:
------------------------------------------------------------------------
[2014-06-05 17:21:56] requinix@php.net
Traits are not inheritance. B was composed of trait A before $_a was set. C came after it was set.
Next time please follow the instructions for creating bug reports. Posting a link for the
description and test script is not enough.
http://bugs.php.net/how-to-report.php
------------------------------------------------------------------------
[2014-06-05 13:31:53] arjen at react dot com
Description:
------------
See http://3v4l.org/nP4Pj
Test script:
---------------
See http://3v4l.org/nP4Pj
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67385&edit=1