


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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software