The difference between static and self in PHP

青灯夜游
Release: 2023-04-08 14:02:01
forward
2375 people have browsed it

The difference between static and self in PHP

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

Using static:: will no longer be resolved For the class in which the current method is defined, it is calculated during 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

";
    }
    public static function callself()
    {
        self::name();
    }

    public static function callstatic()
    {
        static::name();
    }
}

class Man extends Person
{
    public static function name()
    {
        echo "222";
        echo "
"; } } Man::callself(); // output: 111 Man::callstatic(); // output: 222 ?>
Copy after login

Recommended: PHP video tutorial

The above is the detailed content 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:csdn.net
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!