Home>Article>Backend Development> What does php static method refer to?

What does php static method refer to?

藏色散人
藏色散人 Original
2020-07-25 11:04:50 2268browse

php The static method refers to using the static keyword in php to define static methods and attributes. Static can also be used to define static variables and late static binding. Its usage syntax is such as "public static $my_static = 'foo ';".

What does php static method refer to?

Recommended: "PHP Tutorial"

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.

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).

In order to be compatible with PHP 4, if no access control is specified, properties and methods default to public.

Since static methods do not require an object to be called, the pseudo variable $this is not available in static methods.

Static properties cannot be accessed by objects through the -> operator.

Calling a non-static method statically will result in an E_STRICT level error.

Like all other PHP static variables, static properties can only be initialized to literals or constants, not expressions. So a static property can be initialized to an integer or an array, but it cannot be initialized to another variable or function return value, nor can it point to an object.

Since PHP 5.3.0, you can use a variable to dynamically call a class. But the value of this variable cannot be the keywords self, parent or static.

Example #1 Static property example

staticValue() . "\n"; print $foo->my_static . "\n"; // Undefined "Property" my_static print $foo::$my_static . "\n"; $classname = 'Foo'; print $classname::$my_static . "\n"; // As of PHP 5.3.0 print Bar::$my_static . "\n"; $bar = new Bar(); print $bar->fooStatic() . "\n"; ?>    静态方法示例  

The above is the detailed content of What does php static method refer to?. 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