#47384 [Csd->Opn]: 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: Closed
+Status: Open
Bug Type: Documentation problem
Operating System: *
PHP Version: 5.*, 6
New Comment:
Again, this is NOT the difference between self and static, this is the
self keyword behaving like static when it shouldn't.
Previous Comments:
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
[2009-06-14 23:09:24] scottmac@php.net
You can use static:: in PHP 5.3+ this is Late Static Binding.
------------------------------------------------------------------------
[2009-06-14 13:51:01] alreece45 at gmail dot com
Perhaps this is not a Documentation Problem, but a scripting problem? I
set it as documentation because I wasn't sure if this behavior was
intentional or not. If it was intentional, the documentation needed to
be updated because it currently says:
"Static references to the current class like self:: or __CLASS__ are
resolved using the class in which the function belongs, as in where it
was defined"
I'd appreciate it to know, at the very least, if this behavior is
intentional or not for two reasons:
1) Avoid using the "feature" in PHP projects.
2) So I know if I take the time to try make a patch: I know I didn't
just waste my time (even if I fail).
also: updating the summary to be more clear (hopefully)
------------------------------------------------------------------------
[2009-02-13 22:55:01] alreece45 at gmail dot com
Description:
------------
When defining properties using constants with parent or self, they are
resolved using computed classes instead of defined classes.
This behavior appears to have existed since PHP 5.0.5 and still exists
in PHP 5.3beta3, a 5.3 CVS snapshot (200902122000), and 5.2 CVS snapshot
(200902121200).
Someone has brought this up before in php-internals:
http://marc.info/?l=php-internals&m=118839969729862&w=2
Reproduce code:
---------------
<?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';
}
$older = new GrandSon;
echo "{$older->name}\n";
echo "{$older->daddy}\n";
?>
Expected result:
----------------
Father
Father
Actual result:
--------------
Grandchild
Son
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47384&edit=1
Thread (8 messages)