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

The marquee tag in HTML achieves seamless scrolling marquee effect

The <marquee> tag is a tag that appears in ...

CSS hacks \9 and \0 may not work for hacking IE11\IE9\IE8

Every time I design a web page or a form, I am tr...

Vue implements the product tab of the product details page function

This article example shares the specific code of ...

Nginx configuration and compatibility with HTTP implementation code analysis

Generate SSL Key and CSR file using OpenSSL To co...

Details on using JS array methods some, every and find

Table of contents 1. some 2. every 3. find 1. som...

What is the length of a function in js?

Table of contents Preface Why How much is it? Num...

Analysis of the process of deploying Python applications in Docker containers

Simple application deployment 1. Directory struct...

How to check if a table exists in MySQL and then delete it in batches

1. I searched for a long time on the Internet but...

Summary of Common Mistakes in Web Design

In the process of designing a web page, designers...

MySQL should never write update statements like this

Table of contents Preface cause Phenomenon why? A...

Conditional comments to determine the browser (IE series)

<!--[if IE 6]> Only IE6 can recognize <![...

JS implements multiple tab switching carousel

Carousel animation can improve the appearance and...

MySQL data type details

Table of contents 1. Numeric Type 1.1 Classificat...