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

What does php static method refer to?

藏色散人
Release: 2023-03-03 10:22:02
Original
2415 people have browsed it

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

<?php
class Foo
{
    public static $my_static = &#39;foo&#39;;
    public function staticValue() {
        return self::$my_static;
    }
}
class Bar extends Foo
{
    public function fooStatic() {
        return parent::$my_static;
    }
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n";      // Undefined "Property" my_static 
print $foo::$my_static . "\n";
$classname = &#39;Foo&#39;;
print $classname::$my_static . "\n"; // As of PHP 5.3.0
print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>
   </programlisting>
  </example>
  <example>
   <title>静态方法示例</title>
    <programlisting role="php">
<![CDATA[
<?php
class Foo {
    public static function aStaticMethod() {
        // ...
    }
}
Foo::aStaticMethod();
$classname = &#39;Foo&#39;;
$classname::aStaticMethod(); // 自 PHP 5.3.0 起
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template