Home  >  Article  >  Backend Development  >  The difference between static and self in PHP

The difference between static and self in PHP

青灯夜游
青灯夜游forward
2020-04-02 09:43:422464browse

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

<?php
class 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: 111
Man::callstatic();  // output: 222
?>

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete