About the difference between static and self in PHP static class static pronunciation staticlayout

WBOY
Release: 2016-07-29 08:53:29
Original
1075 people have browsed it

I have seen it once before, but I forgot the specific difference between static and self. I will review it this time.

​ static was added after PHP5.3, take a look at the English explanation:

​ self refers to the same class whose method the new operation takes place in.

​ static in PHP 5.3's late static bindings refers to whatever class in the hierarchy which you call the method on.

I have seen an article before that has a good example:

class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        self::who();
//        static::who();
    }
}
A::test();

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}
echo B::test();
Copy after login

self: static method, pointing to the class that exists itself

static: pointing to the class that calls it

It’s still easy to understand, don’t forget it next time.

The above introduces the difference between static and self in PHP, including static content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!