Briefly talk about php delayed static binding
Usage scenario
First observe the following code:
abstract class base { //do sth } class aClass extends base{ public static function create(){ return new aClass(); } } class bClass extends base{ public static function create(){ return new bClass(); } } var_dump(aClass::create()); var_dump(bClass::create());
Output:
object(aClass)#1 (0) { } object(bClass)#1 (0) { }
The above aClass and bClass inherit from the abstract class base, but the static method create() is implemented in both subclasses at the same time. Following the oop idea, this repetitive code should be implemented in the parent class base.
Improved code
abstract class base { public static function create(){ return new self(); } } class aClass extends base{ } class bClass extends base{ } var_dump(aClass::create()); var_dump(bClass::create());
The current code seems to be in line with our previous ideas. The create() method is shared in the parent class. Let's run it and see what happens.
Cannot instantiate abstract class base in...
Unfortunately, the code does not seem to run as we expected. self() in the parent class is parsed to the parent class base, and does not inherit from his children. kind. So in order to solve this problem, the concept of delayed static binding was introduced in php5.3.
Delayed static binding
abstract class base { public static function create(){ return new static(); } } class aClass extends base{ } class bClass extends base{ } var_dump(aClass::create()); var_dump(bClass::create());
This code is almost the same as the previous one. The difference is that self is replaced with the static keyword. static will be resolved to a subclass instead of a parent class, so that the above problem can be solved. The problem I encountered is PHP's delayed static binding.
Finally, run the code and get the final desired result.
object(aClass)#1 (0) { } object(bClass)#1 (0) { }
The above has introduced a brief talk about PHP delayed static binding, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The Velodrome model is inspired by veCRV and aims to achieve superior consistency among the three key participants of DEX, including liquidity providers (LPs), token holders, and projects that require liquidity. However, many participants in the DeFi field still do not fully understand the underlying reasons. By reading this article in-depth, you will be able to get out of this dilemma and get to the bottom of it. Today we will discuss Velodrome/Aerodrome, a real success story in the DeFi field. This article will compare the two models and explain how Velodrome improves on the veCRV model and what significant impact these small differences have. First, let me state

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

In Golang programming, it is a relatively common requirement to use regular expressions to verify whether the input is a legal base64 string. For developers, regular expressions can be used to quickly and accurately verify whether user input is correct. This article will introduce how to use regular expressions in Golang to verify whether the input is a legal base64 string. Starting with basic syntax In Golang, using regular expressions requires using the "regexp" library. This library provides "Compile" and "

In Java programming, it is often necessary to convert binary data into text format for transmission, and Base64 encoding is a commonly used conversion method. Base64 converts three bytes of data into four bytes of text data. The text data consists of 64 characters. It only contains printable characters, so it can be transmitted in protocols such as emails and HTTP request messages. Java provides Base64 encoding and decoding APIs, so we can easily convert data. This article will introduce how to use in Java

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

Background Recently, key business codes have been encrypted for the company framework to prevent the engineering code from being easily restored through decompilation tools such as jd-gui. The configuration and use of the related obfuscation scheme are relatively complex and there are many problems for the springboot project, so the class files are encrypted and then passed The custom classloder is decrypted and loaded. This solution is not absolutely safe. It only increases the difficulty of decompilation. It prevents gentlemen but not villains. The overall encryption protection flow chart is shown in the figure below. Maven plug-in encryption uses custom maven plug-in to compile. The class file specified is encrypted, and the encrypted class file is copied to the specified path. Here, it is saved to resource/corecla.

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.
