Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between final and static in php

Shulou Source: shulou.com Published: 2022-06-03 13:17:10 09月27日 Update

Editor to share with you what is the difference between final and static in php, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The difference between final and static in php: 1, static is a static variable, static variable only exists in the local function domain, but when the program execution leaves this scope, its value is not lost; 2, final is used for the function, belonging to object-oriented usage, once used, this function cannot be changed and overloaded.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer

The difference between final and static in php

Static is a static variable, which exists only in the local function domain, but its value is not lost when the program execution leaves this scope.

For example, you can use it to calculate the cumulative number of function calls.

Static (static) keywords

This page explains using the static keyword to define static methods and properties. Static can also be used to define static variables and later static bindings. See the above page for the use of static in it.

If a class property or method is declared static, it can be accessed directly without instantiating the class. Static properties cannot be accessed through an object instantiated by a class (but static methods can).

Static method

Because static methods do not need to be called through an object, the pseudo variable $this is not available in static methods.

Calling a non-static method statically throws an Error.

Prior to PHP 8.0.0, the use of calling a non-static method statically had been 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 and overloaded.

PHP 5 adds a final keyword. If a method in the parent class is declared as final, the subclass cannot override the method. If a class is declared as final, it cannot be inherited.

These are all the contents of this article entitled "what's the difference between final and static in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Static method function variable object key keyword property article function content instance local mode program not much most subclass clear more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi vpn MariaDB macOS