


Detailed explanation of the properties and methods of PHP static variables and the usage of delayed binding
The type keyword of static variables is static. This article is mainly about learning how to use static properties and static methods in PHP and basic examples as well as delayed binding
Static (static) keyword is used to define static methods and properties, static can also be used to define static variables and Late static binding.
1, static variable static variable
Static variables only exist in the local function scope, but when the program execution leaves this scope, its value is not lost. That is to say, the variable will still remember its original value the next time this function is executed. To define a variable as static, just add the static keyword before the variable.
function testing() { static $a = 1; $a *= 2; echo $a."\n"; } testing(); testing(); testing(); testing(); /** * 2 * 4 * 8 * 16 * [Finished in 0.1s] */
Static variables also provide a way to deal with recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, as they may recurse indefinitely. You must ensure that there are adequate ways to terminate recursion.
In this example, the testing() function will internally save the value of the $a variable after each execution. The next time testing() is called, the value of $a is restored, and testing() multiplies this value by 2 and prints it. The initial default value of a variable is 1, and this assignment only occurs the first time the variable is initialized. This operation will not be called during each execution of the function.
2, Use of static elements in classes
There are two main uses of static keyword in classes, one is used to define static members, the other is Kind is used to define static methods. Declaring a class property or method static allows you to access it directly without instantiating the class. Static properties cannot be accessed through an object of a class that has been instantiated (but static methods can). Static properties cannot be accessed by objects through the -> operator. Inside a class we can use scope qualification operators to access variables at different levels of scope.
2.1, Static properties
Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods. Static variables can be thought of as belonging to the entire class rather than to an instance of the class. Different from general instance variables, static properties only retain one variable value, and this variable value is valid for all instances, which means that all instances share this property.
class MyObject { public static $a = 0; function MyMethod() { self::$a += 2; echo self::$a . "\n"; } } $instance1 = new MyObject(); $instance1 -> MyMethod(); $instance2 = new MyObject(); $instance2 -> MyMethod(); /** * * 2 * 4 * [Finished in 0.1s] * */
$this indicator is the current instance of the class and is a reference to the calling object.
self:: represents the class itself. When using self:: scope qualifier, you must add the $ symbol after the operator. This cannot be used in code outside the class. operator, and it does not recognize its position in the inheritance tree hierarchy. When using self:: scope in an extended class, self can call methods declared in the base class, but it will always call methods that have been overridden in the extended class.
parent:: In the extended class, when the base class method is overridden, if you want to access the base class method, you can use parent::
static:: to use We no longer need to use self:: and parent::. You can use static when you want to point to the final class that implements the functionality. This qualifier will calculate the members of the last class in the inheritance hierarchy immediately before the code is executed.
2.3, Static method
The rules for static methods are the same as static variables. A method can be marked as static using the static keyword, and static methods can be accessed through the name of the class and the scoping operator (::).
There is an important difference between static methods and non-static methods: when calling a static method, we no longer need to own an instance of the class.
class MyObjectBase { static function MyMethod() { static::MyOtherMethod(); } static function MyOtherMethod() { echo 'called from MyObject.'; } } class MyExtendObject extends MyObjectBase { static function MyOtherMethod() { echo 'called from MyExtendObject.'; } } MyExtendObject::MyMethod();
The above example code will correctly call the MyOtherMethod method in MyExtendObject and output called from MyExtendObject. [Finished in 0.1s].
If a method does not contain the $this variable, then this method should be a static method. If you don't need an instance of a class, you should also use a static class, which eliminates the need for instantiation. In addition, the $this variable cannot be used in static methods because static methods do not belong to a specific instance.
2.4, Lazy binding
static:: eliminates the need for us to use self:: and parent:: . You can use static when you want to point to the final class that implements the functionality. This qualifier will calculate the members of the last class in the inheritance hierarchy immediately before the code is executed. This process is called delayed binding.
3, Summary
Use the static keyword to create static variables and provide a default initialization value. Static variables are modified function variables whose value remains unchanged after a function is executed.
Thestatic keyword can also be used in a class to modify properties and methods. When used on a property, it causes the property to no longer hold a value for a certain instance, but instead holds a value for the entire class itself. Static properties can be shared among members.
To access static methods, you can use (::), which is called the scope qualifier. The left side of this operator can be a class name or a predefined scope. The predefined scope includes self parent static. The right side of the operator is a static method and property.
The above is the detailed content of Detailed explanation of the properties and methods of PHP static variables and the usage of delayed binding. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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





PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
