php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #26930 Inheritance of static functions
Submitted: 2004-01-16 04:58 UTC Modified: 2004-09-30 20:23 UTC
From: reckert at informationsgesellschaft dot com Assigned: helly (profile)
Status: Not a bug Package: Documentation problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2004-01-16 04:58 UTC] reckert at informationsgesellschaft dot com
Description:
------------
In a static function X a call to another static function in the same class uses the version of the function in the class where X is implemented, not the one for which X was called for.


Reproduce code:
---------------
class a {
	static function init() {
		echo 'a::init()';
	}

	static function callInit() {
		self::init();
		// do something important
	}
}

class b extends a {
	static function init() {
		echo 'b::init()';
	}
}

b::callInit();

Expected result:
----------------
b::init()

Actual result:
--------------
a::init()


Shouldn't inheritance work the same way as for normal methods?

Thanks for comments,
Ralph

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-16 05:08 UTC] helly@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

inside callInit self === a
 [2004-05-21 05:23 UTC] kell_pt at users dot sf dot net
I beg to differ. If you call b::callInit() and b:: has init() redefined, it SHOULD call the b::init when refering to self, regardless of where that "self" is included.

Basically, "self" must take in consideration the caller. This is not the only hint that the class mechanism is flawed in what concerns to static methods / properties. Static data and methods should follow the same concepts from class instances - it's how OOP works - change that and you can't really claim it's OOP. :)
 [2004-05-21 10:17 UTC] abies@php.net
Dynamic binding has nothing to do with the caller of a 
method/function, it is based on the [runtime] class of the 
object instance whose method is being called.   
 
Static methods do no operate on object instances, so 
dynamic binding is impossible. No compiled OO language 
does this. For PHP, it would also break BC. 
 [2004-09-30 18:22 UTC] kell_pt at users dot sf dot net
I also filed a bug regarding this problem.

It's not so much a problem as how it works, but how it SHOULD work. It is counter-intuitive the way inheritance works on static methods, and should be addressed at least with a mention in the manual.
 [2004-09-30 20:23 UTC] helly@php.net
Fixed in documentation (bla dot)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 15 19:01:26 2024 UTC