By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,719 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,719 IT Pros & Developers. It's quick & easy.

PHP Limitation: Static Inheritance and Scope

P: n/a
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?

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
wwight wrote:
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?


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.
js*******@attglobal.net
==================
Jul 17 '05 #2

P: n/a
wwight wrote:
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?
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

Jul 17 '05 #3

P: n/a
wwight wrote:
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?
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

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.