PHP 5 scope resolution problem

I was working a lot with PHP 4, with PHP5 I work for not very long time. For now the most disappointing thing in PHP 5 is scope resolution problem. It is described in ian at [first name]henderson dot org comment:

Please note that methods called by the scope resolution operator which are defined by a superclass of the first operand are called in the scope of the SUPERCLASS. For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
 
class ExampleSuperclass
{
    static function classType()
    {
        return "superclass";
    }
 
    static function doSomething()
    {
        echo "doing something with " . self::classType();
    }
}
 
class ExampleClass extends ExampleSuperclass
{
    static function classType()
    {
        return "subclass";
    }
}
 
ExampleClass::doSomething();
// output is "doing something with superclass"!
 
?>

This can be surprising (it surprised me!) when coming from other object-oriented languages, which would output “doing something with subclass” in this case.

The answer was:

The functionality you’ve expected maybe will be possible in PHP6….

That’s pity.

This entry was posted on Tuesday, May 6th, 2008 at 21:08 and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.





2 Responses to “PHP 5 scope resolution problem”

Appreciate you post. Friends send me a link. It’s very interesting. Subscribed on RSS! Will advice to my friends!

Leave a Reply


required


required (will not be published)



CAPTCHA image