Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PHP Limitation: Static Inheritance and Scope

1 view
Skip to first unread message

wwight

unread,
Jun 4, 2005, 2:20:20 PM6/4/05
to
As many people have noticed by now, PHP exhibits some frustrating
behavior when it comes to static fields and methods. For instance,
when a static method is defined in a parent class, but called from a
child class, the method uses the parent class for scope (not the
originating child class). For example, see the following block of
code:

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?

Jerry Stuckle

unread,
Jun 4, 2005, 3:47:51 PM6/4/05
to

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
==================

Oli Filth

unread,
Jun 4, 2005, 2:51:30 PM6/4/05
to

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

Oli Filth

unread,
Jun 4, 2005, 2:52:23 PM6/4/05
to

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

0 new messages