Bug #54543 [Bgs]: Late Static Binding on Properties and/or Inheritance
| From: | cataphract@php.net | Date: | Sat, 16 Apr 2011 01:44:40 +0000 |
| Subject: | Bug #54543 [Bgs]: Late Static Binding on Properties and/or Inheritance | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-160202@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54543&edit=1
ID: 54543
Updated by: cataphract@php.net
Reported by: bmajdak@php.net
Summary: Late Static Binding on Properties and/or Inheritance
Status: Bogus
Type: Bug
Package: Class/Object related
Operating System: Any
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
Changing this behavior would be a BC break; consider:
<?php
class A {
public static $f = 7;
}
class B extends A {
function m() {
A::$f = 6;
return self::$f;
}
}
Previous Comments:
------------------------------------------------------------------------
[2011-04-16 03:24:22] bmajdak@php.net
you are correct in your statement about breaking the reference, the
following
example works as i believe should be default.
<?php
class old {
static $prop = 'old';
public function __construct() {
static::$prop = &$this;
// ^^^ daft assignment of any random reference to cause it
// to break itself. normal assignment will not do it has to
// be a reference. comment out that line and instead of
// "old one two" you will get "two two two"
static::$prop = get_class($this);
// ^^^ an actual assignment that i care about keeping.
return;
}
}
class one extends old { }
class two extends old { }
$one = new one;
$two = new two;
printf("value: %s %s %s\n",old::$prop,one::$prop,two::$prop);
?>
nice and jenky.
------------------------------------------------------------------------
[2011-04-16 01:37:22] cataphract@php.net
Expected behavior. LSB applies to properties, but in PHP it was decided
to have static properties shared between classes and their subclasses
(via a reference set), possibly so that they can reliably be used in a
non-static contexts. This behavior can be changed by redeclaring the
property or manually breaking the reference set (possibly this ought to
be documented).
------------------------------------------------------------------------
[2011-04-15 22:46:25] bmajdak@php.net
Description:
------------
Should late static binding also apply to properties? My test here I
would have
expected each result to be different, not the value of the last created
object. It
is counter-intuitive to work on methods and not properties. I would
expect this
behaviour with self:: but not static::
inb4 "we rejected this multiple times" i do not actually care about the
object
class name like in my example, that was just an easy demo case.
Test script:
---------------
<?php
// [bob@cairne:test]$ php static.php
// value: two two two
class old {
static $prop = 'old';
public function __construct() { static::$prop = get_class($this); }
}
class one extends old { }
class two extends old { }
$one = new one;
$two = new two;
printf("value: %s %s %s\n",old::$prop,one::$prop,two::$prop);
?>
Expected result:
----------------
value: old one two
Actual result:
--------------
value: two two two
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54543&edit=1