class Parent {
private static $className = 'Parent';
public static function getName() {
return self::$className . "\n";
}
}
class Child extends Parent {
private static $className = 'Child';
}
echo Parent::getName();
echo Child::getName();
------------------------------
Intuitive Results
------------------------------
Parent
Child
------------------------------
Actual Results
------------------------------
Parent
Parent
Does anyone know if the PHP dev team has plans to remedy this issue? I
personally feel that the inability to handle static scoping with
inheritance is a major limitation to the OO functionality of PHP. I've
seen several relevant bug reports on bugs.php.net, but most have been
categorized as Bogus. One member of the dev team flat out insulted one
of the bug reporters, saying "Sorry, you have a complete wrong idea og
OO programming. [sic]"
http://bugs.php.net/bug.php?id=30140
http://bugs.php.net/bug.php?id=30423
http://bugs.php.net/bug.php?id=30934
http://bugs.php.net/bug.php?id=30235
However, reading through the various bug reports, I wonder if the dev
team may be softening just a bit in their resistance more recently. I
recommend voting for the bugs above, submitting more functionaity
requests, and continuing discussion in developer forums. Short of
fixing it ourselves, what else can we do to convince the PHP dev team
to address this problem?
In this case I agree with the PHP developers.
The parent class knows nothing about the child class; in fact, there may
not even be a child class. The parent class's methods need to use only
what's available in the parent class.
As an example - C++ exhibits this same behavior. The C++ spec has added
some features for dynamic typing of classes, but the static typing works
this same way.
If your design has the parent class depending on something in the child
class, then your design is broken. The child class should always extend
the parent class or make a more specific instance of a parent class, not
vice versa.
In your case, if you want the function to be defined in the scope of the
child class, you need to specify the function in the child class.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================
There is no issue to resolve. See below...
> I personally feel that the inability to handle static scoping with
> inheritance is a major limitation to the OO functionality of PHP. I've
> seen several relevant bug reports on bugs.php.net, but most have been
> categorized as Bogus. One member of the dev team flat out insulted one
> of the bug reporters, saying "Sorry, you have a complete wrong idea og
> OO programming. [sic]"
They are correct!
The same occurs in Java and C++.
By definition, static methods are given no "this" pointer. So when you
call Child::getName(), it resolves to a call to Parent::getName(). The
only scope that Parent::getName() has is the static members of Parent.
--
Oli
There is no issue to resolve. See below...
> I personally feel that the inability to handle static scoping with
> inheritance is a major limitation to the OO functionality of PHP. I've
> seen several relevant bug reports on bugs.php.net, but most have been
> categorized as Bogus. One member of the dev team flat out insulted one
> of the bug reporters, saying "Sorry, you have a complete wrong idea og
> OO programming. [sic]"
They are correct!
The same occurs in Java and C++.
By definition, static methods are given no "this" pointer. So when you
call Child::getName(), it resolves to a call to Parent::getName(). The
only scope that Parent::getName() has is the static members of Parent.
The sort of behaviour that you expect is not static.
--
Oli