A brief analysis of the difference between static and self in PHP classes

A brief analysis of the difference between static and self in PHP classes

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

Using static:: is no longer resolved to the class where the current method is defined, but is actually calculated at runtime. It can also be called "static binding" because it can be used for (but is not limited to) calling static methods.

Static binding is a feature added in PHP 5.3.0 that is used to reference statically called classes in the inheritance scope.

The difference between static and self in PHP classes. We often see self and static in the definition of PHP classes. When running, we often find that the results are no different, but there is definitely a difference. If there is no difference, why are there two?

1. The difference between the two

Static uses delayed binding

Therefore, static can accurately know whether it is a call from the parent class or the child class.

For example, this shows that Static is a smart kid. He can accurately recognize all his relatives.

Self is a little bit stupid, he only knows your own parents

2. Code looks different

It can be seen from the following code

self refers to the class that defines him, that is, he only knows his own parents

static refers to the class that calls him, that is, he can recognize his relatives

<?php
class parents
{
    protected static $name = 'I am your parent';
 
    public static function who_self()
    {
        return self::$name;
    }
 
    public static function who_static()
    {
        return static::$name;
    }
 
    public static function get_self()
    {
        return new self();
    }
 
    public static function get_static()
    {
        return new static();
    }
}
 
class kinsfolk extends parents
{
    protected static $name = 'I am your relative';
}
 
var_dump(kinsfolk::who_self()); //I am your parentvar_dump(kinsfolk::who_static()); //I am your relativevar_dump(kinsfolk::get_self()); //object(parents)#1 (0) {}
var_dump(kinsfolk::get_static()); //object(kinsfolk)#1 (0) {}

In simple terms,

Self is the class in which it is written, and this class is actually called.

static represents the class being used, that is, the static you wrote in the parent class, and then it is overwritten by the subclass, and the method or attribute of the subclass is used

Summarize

This is the end of this article about the difference between static and self in PHP class. For more information about the difference between static and self in PHP class, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • PHP implements two ways of scheduling classes
  • Detailed analysis of the differences between PHP7 and PHP5
  • Two ways to generate user passwords in PHP
  • How to use PDO in PHP to operate large data objects
  • PHP uses the Workman framework to implement socket services and connect to clients

<<:  Modify the boot time of grub in ubuntu

>>:  Explanation of MySQL index types Normal, Unique and Full Text

Recommend

Implementing file content deduplication and intersection and difference in Linux

1. Data Deduplication In daily work, there may be...

Several ways to solve the 1px border problem on mobile devices (5 methods)

This article introduces 5 ways to solve the 1px b...

MySQL 8.0.15 compressed version installation graphic tutorial

This article shares the installation method of My...

Detailed explanation of the practical application of centos7 esxi6.7 template

1. Create a centos7.6 system and optimize the sys...

Advanced crawler - Use of Scrapy_splash component for JS automatic rendering

Table of contents 1. What is scrapy_splash? 2. Th...

CentOS system rpm installation and configuration of Nginx

Table of contents CentOS rpm installation and con...

js dynamically adds example code for a list of circled numbers

1. Add the ul tag in the body first <!-- Unord...

Specific use of Linux which command

We often want to find a file in Linux, but we don...

Detailed steps to install MySQL 5.7 via YUM on CentOS7

1. Go to the location where you want to store the...

How to quickly paginate MySQL data volumes of tens of millions

Preface In backend development, in order to preve...

Json advantages and disadvantages and usage introduction

Table of contents 1. What is JSON 1.1 Array liter...

Detailed tutorial for downloading, installing and configuring MySQL 5.7.27

Table of contents 1. Download steps 2. Configure ...