What is the difference between PHP Class self and static? How to use them?

藏色散人
Release: 2023-04-10 16:40:01
forward
3829 people have browsed it

For most PHPers, the two PHP keywords self and static are not unfamiliar. We learn to call the static properties and methods of the current class through self::xxxx. And what about static? Many people must know that it is used to define a static method and class attribute keywords.

This is also my previous understanding.

Now let’s review some common uses of these two keywords:

// self 用法 1 :调用静态成员属性
<?php
class Person
{
    protected static $maxAddressCount = 5; // 收获地址创建最大数量。

    public function test()
    {
        echo self::$maxAddressCount;
    }
}

$person = new Person();
$person->test();
Copy after login
// self 用法 2 :调用静态方法
<?php
class Person
{
    protected static $maxAddressCount = 5; // 收获地址创建最大数量。

    protected static function getMaxAddressCount()
    {
        return self::$maxAddressCount;
    }

    public function test()
    {
        echo self::getMaxAddressCount();
    }
}

$person = new Person();
$person->test();
Copy after login
// self 用法 3 :创建一个当前对象
<?php
// 单例示例
class Person
{
    private static $instance = null;

    private function __construct() {}

    final public static function getInstance() 
    {
        if (self::$instance == null) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function test()
    {
        echo "hello world!";
    }
}

$person = Person::getInstance();
$person->test();
Copy after login
The common uses of the static keyword are also comprehensively reflected in the above three examples.

I firmly believe that the above usage is very familiar to any entry-level PHPer. Now what I want to talk about is the following two ways:

new self() 与 new static() 的区别?
Copy after login

I believe many people know that new self() creates an object of the current class, but they don’t know that new static( ) can also create an object of the current class.

Regarding the usage of new static(), it is explained in the official documentation. Address: https://www.php.net/manual/zh/language.oop5.late-static-bindings.php

PHP officially calls this method: late static binding.

Official example 1:

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

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();
Copy after login

Because Class B inherits Class A. Both A and B have a static method who(). At this time, when passing B::test(), what is actually called is the who() method of Class A.

Because the static method who() of subclass Class B is defined in the subclass after Class A. The default feature of PHP only allows the first defined one to be called.

This leads to the concept of late static binding.

Official example 2:

<?php
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // 后期静态绑定从这里开始
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();
Copy after login

We change the test() method body in Class A to static## After #, static represents always pointing to the calling class. That is to say, although the method defined in the Class A parent class conflicts with the subclass with the same name. However, when the subclass calls it, it automatically switches to the subclass's static method with the same name. Depends on the caller. You can understand by running the above two examples.

The reason why there is this section. This problem occurred because I had to inherit the singleton method during actual operation. Therefore, this feature is involved.

Recommended learning: "

PHP Video Tutorial
"

The above is the detailed content of What is the difference between PHP Class self and static? How to use them?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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!