Home  >  Article  >  Backend Development  >  What is the difference between final and static in php

What is the difference between final and static in php

藏色散人
藏色散人Original
2021-09-16 09:32:221323browse

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.

What is the difference between final and static in php

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!

Statement:
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