#47384 [Opn->Bgs]: static references resolved incorrectly with class inheritance

From: Date: Sun, 10 Jan 2010 13:37:02 +0000
Subject: #47384 [Opn->Bgs]: static references resolved incorrectly with class inheritance
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-3665@lists.php.net to get a copy of this message
 ID:               47384
 Updated by:       degeberg@php.net
 Reported By:      alreece45 at gmail dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         Documentation problem
 Operating System: *
 PHP Version:      5.*, 6
 New Comment:

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";
?>


Previous Comments:
------------------------------------------------------------------------

[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.

------------------------------------------------------------------------

[2009-06-14 23:09:24] scottmac@php.net

You can use static:: in PHP 5.3+ this is Late Static Binding.

------------------------------------------------------------------------

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)

« previous php.doc.bugs (#3665) next »