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

Some common mistakes with MySQL null

According to null-values, the value of null in My...

Vue implements top left and right sliding navigation

Navigation and other things are often used in dai...

JavaScript to switch multiple pictures

This article shares the specific code of JavaScri...

Linux 6 steps to change the default remote port number of ssh

The default ssh remote port in Linux is 22. Somet...

JavaScript object-oriented implementation of magnifying glass case

This article shares the specific code of JavaScri...

The difference between Display, Visibility, Opacity, rgba and z-index: -1 in CSS

We often need to control the hidden, transparent ...

How to Enable or Disable Linux Services Using chkconfig and systemctl Commands

This is an important (and wonderful) topic for Li...

Understand the initial use of redux in react in one article

Redux is a data state management plug-in. When us...

Detailed explanation of two points to note in vue3: setup

Table of contents In vue2 In vue3 Notes on setup ...

Six important selectors in CSS (remember them in three seconds)

From: https://blog.csdn.net/qq_44761243/article/d...

Detailed explanation of Docker basic network configuration

External Access Randomly map ports Using the -P f...