Re: self in inherited methods
| From: | Stuart | Date: | Tue, 21 Apr 2009 10:22:43 +0000 |
| Subject: | Re: self in inherited methods | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-291723@lists.php.net to get a copy of this message | ||
2009/4/19 Alex S Kurilo aka Kamazee <me@kamazee.name>:
> Is it right that 'self' in inherited method still points to the parent?
> If it is, can you explain it? It makes me worry :)
>
> A piece of code below for example
>
> <?php
> class MyParent {
> const NAME = 'MyParent';
> public function get_instance() {
> return new self;
> }
> public function get_another_instance() {
> $class_name = get_class($this);
> return new $class_name;
> }
> public function get_name() {
> return self::NAME;
> }
> }
>
> class MyClass extends MyParent {
> const NAME = 'MyClass';
> }
>
> $a = new MyClass;
> $b = $a->get_instance();
> $c = $a->get_another_instance();
>
> echo $a->get_name(),"\n";
> echo get_class($b),"\n";
> echo get_class($c),"\n";
> ?>
Up until 5.3 this was just the way it was. It has been "fixed" in 5.3.
See here for more info: http://php.net/lsb
-Stuart
--
http://stut.net/