Home >Backend Development >PHP Problem >What is the difference between final and static in php
The difference between final and static in php: 1. static is a static variable. Static variables only exist in the local function domain, but when the program execution leaves this scope, its value is not lost; 2. final It is used for functions and belongs to object-oriented usage. Once used, this function cannot be changed or overloaded.
The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer
The difference between final and static in php
static is a static variable. Static variables only exist in the local function scope, but when the program execution leaves this scope, its value is not lost.
For example, you can use it to count the cumulative number of function calls.
<?PHP function Test() { static $w3sky = 0; echo $w3sky; $w3sky++; } ?>
Static (static) keyword
This page explains the use of the static keyword to define static methods and properties. static can also be used to define static variables and late static binding. See the above page to see how static is used there.
Declaring class attributes or methods as static allows direct access without instantiating the class. Static properties cannot be accessed through an object of a class that has been instantiated (but static methods can).
Static method
Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods.
Calling a non-static method statically will throw an Error.
Prior to PHP 8.0.0, statically calling a non-static method was deprecated and resulted in an E_DEPRECATED level warning.
fainal is used for functions and belongs to object-oriented usage. Once used, this function cannot be changed or overloaded.
PHP 5 adds a new final keyword. If a method in the parent class is declared final, the child class cannot override the method. If a class is declared final, it cannot be inherited.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between final and static in php. For more information, please follow other related articles on the PHP Chinese website!