Detailed explanation of the difference between static and self in php

黄舟
Release: 2023-03-14 20:32:01
Original
1502 people have browsed it

I was asked about it during the interview, but I had no choice but to give an answer. I would like to summarize it here.

Use self:: or __CLASS__A static reference to the current class, depending on the class in which the current method is defined:

Using static:: is no longer used Resolves to the class in which the current method is defined, but is calculated at actual runtime. It can also be called "static binding" because it can be used for (but is not limited to) calls to static methods.

Static binding is a function added in PHP 5.3.0 for referencing statically called classes in the inheritance scope

In simple terms,

self refers to which class it is written in, and it is this class that is actually called.

static represents the class used, which is the static you write in the parent class, and then it is overridden by the subclass. What is used is the method or attribute of the subclass

<?phpclass Person{
    public static function name()
    {
        echo "111";        echo "<br />";
    }    public static function callself()
    {
        self::name();
    }    public static function callstatic()
    {
        static::name();
    }
}class Man extends Person{
    public static function name()
    {
        echo "222";        echo "<br />";
    }
}

Man::callself();  // output: 111Man::callstatic();  // output: 222?>
Copy after login

The above is the detailed content of Detailed explanation of the difference between static and self in php. For more information, please follow other related articles on the PHP Chinese website!

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!