Home Backend Development PHP Tutorial Briefly talk about php delayed static binding

Briefly talk about php delayed static binding

Jul 29, 2016 am 09:04 AM
base class create

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());
Copy after login

Output:

object(aClass)#1 (0) { } object(bClass)#1 (0) { }
Copy after login
Copy after login

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());
Copy after login

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());

Copy after login

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) { }
Copy after login
Copy after login

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Base DEX Faucet: Aerodrome VS Curve Base DEX Faucet: Aerodrome VS Curve Mar 26, 2024 pm 04:31 PM

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

How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

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)

Use regular expressions in golang to verify whether the input is a legal base64 string Use regular expressions in golang to verify whether the input is a legal base64 string Jun 24, 2023 am 10:01 AM

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 "

How to encode and decode using Base64 functions in Java How to encode and decode using Base64 functions in Java Jun 26, 2023 pm 02:24 PM

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

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

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

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

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()].

How SpringBoot encrypts and protects class files through custom classloader How SpringBoot encrypts and protects class files through custom classloader May 11, 2023 pm 09:07 PM

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.

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

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.

See all articles