Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

php - Overriding class constants vs properties

I would like to better understand why, in the scenario below, there is a difference in the way class constants are inherited vs. instance variables.

<?php
class ParentClass {
    const TEST = "ONE";
    protected $test = "ONE";

    public function showTest(){
        echo self::TEST;
        echo $this->test;
    }
}

class ChildClass extends ParentClass {
    const TEST = "TWO";
    protected $test = "TWO";

    public function myTest(){
        echo self::TEST;
        echo $this->test;
    }
}

$child = new ChildClass();
$child->myTest();
$child->showTest();

Output:

TWO
TWO
ONE
TWO

In the code above, ChildClass does not have a showTest() method, so the ParentClass showTest() method is used by inheritance. The results show that since the method is executing on the ParentClass, the ParentClass version of the TEST constant is being evaluated, whereas because it's evaluating within the ChildClass context via inheritance, the ChildClass member variable $test is being evaluated.

I've read the documentation, but can't seem to see any mention of this nuance. Can anyone shed some light for me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

self:: Isn't inheritance-aware and always refers to the class it is being executed in. If you are using php5.3+ you might try static::TEST as static:: is inheritance-aware.

The difference is that static:: uses "late static binding". Find more information here:

http://php.net/manual/en/language.oop5.late-static-bindings.php

Here's a simple test script I wrote:

<?php

class One
{
    const TEST = "test1";

    function test() { echo static::TEST; }
}
class Two extends One
{
    const TEST = "test2";
}

$c = new Two();

$c->test();

output

test2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...