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.





8 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!

This is a great post. Thank you and Google for chance to read it.

Neike Taika-Tessaro

Just a heads-up for those not yet in the know: http://php.net/manual/en/language.oop5.late-static-bindings.php – runtime scope resolution was added with PHP 5.3 with the keyword static:: as opposed to self:: (which resolves the scope at compile time).

T-Shirts and Fleece. We believe our prints and designs distinguish our t-shirts and fleece from those of our competitors and are staple items for our consumers. The majority of these items display a distinctive art style, utilizing unique treatments, placements of screened images, designs and embroideries. On some of our t-shirts and fleece, we promote our Featured Artist Series, a program in which we work closely with boardsports athletes and relevant artists associated with our target market to design certain products. Most pieces prominently display the Volcom name or the Volcom Stone logo. foakleys http://pinterest.com/foakleys/activity/

ventura seo…

simple » Blog Archive » PHP 5 scope resolution problem…

myositis venus williams autoimmune disease…

simple » Blog Archive » PHP 5 scope resolution problem…

polymyositis difference between occupational therapy and physical therapy…

simple » Blog Archive » PHP 5 scope resolution problem…

Leave a Reply


required


required (will not be published)