#47384 [Bgs]: static references resolved incorrectly with class inheritance
ID: 47384
User updated by: alreece45 at gmail dot com
Reported By: alreece45 at gmail dot com
Status: Bogus
Bug Type: Documentation problem
Operating System: *
PHP Version: 5.*, 6
New Comment:
Again, not bogus, the value of the property $name depends on the order
the objects are created.
Even if the expected behavior makes late static binding possible with
the self keyword instead of the static keyword. That use is not yet
documented, and then the following would become unexpected as it now
displays "Father, Father" both times (instead of "Son, Grandson" when
an instance of Father is not made).
The value of the property should not depend on the order of the
objects initiated.
New Reproduce Code (Does not replace the previous test script, the
original test script output is still the same, and the bug still
holds):
<?php
class Father {
const my_name = 'Father';
public $name = self::my_name;
}
class Son extends Father {
const my_name = 'Son';
public $daddy = parent::my_name;
}
class GrandSon extends Son {
const my_name = 'Grandchild';
}
$temp = new Father;
$older = new GrandSon;
echo "{$older->name}\n"; // outputs Father
echo "{$older->daddy}\n"; // outputs Father
?>
Previous Comments:
------------------------------------------------------------------------
[2010-01-10 13:37:01] degeberg@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
This is expected behavior. Note that there is a difference between
class constants and static class properties. Regular, non-static
properties are set when an object is constructed.
Upon construction of a GrandSon object, self::my_name would
'Grandchild' and parent::my_name would contain 'Son'. You can achieve
the kind of behavior you're looking for using static properties:
<?php
class Father {
public static $my_name = 'Father';
public $name;
public function __construct() {
$this->name = self::$my_name;
}
}
class Son extends Father {
static $my_name = 'Son';
public $daddy;
public function __construct() {
parent::__construct();
$this->daddy = parent::$my_name;
}
}
class GrandSon extends Son {
static $my_name = 'Grandchild';
}
$older = new GrandSon;
echo "{$older->name}\n";
echo "{$older->daddy}\n";
?>
------------------------------------------------------------------------
[2009-11-23 14:54:31] vrana@php.net
If the variable is initialized in the constructor then it gives the
expected output:
function __construct() {
$this->name = self::my_name;
}
So this is specific to initialization in the declaration.
------------------------------------------------------------------------
[2009-11-20 13:40:24] alreece45 at gmail dot com
Again, this is NOT the difference between self and static, this is the
self keyword behaving like static when it shouldn't.
------------------------------------------------------------------------
[2009-11-20 10:01:01] vrana@php.net
Difference between self:: and static:: is already documented.
------------------------------------------------------------------------
[2009-06-15 14:57:35] alreece45 at gmail dot com
In the example self is behaving as static does. It should not be doing
that. It contradicts the documentation.
In the father class: $name is set as self::my_name, which is "Father".
In the child classes, even though $name is not set, it goes to
Son::my_name and GrandSon::my_name.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/47384
--
Edit this bug report at http://bugs.php.net/?id=47384&edit=1
Thread (8 messages)