Turn on PHP Static keyword tour mode, static keyword_PHP tutorial

WBOY
Release: 2016-07-12 09:04:59
Original
783 people have browsed it

Enable PHP Static keyword tour mode. The static keyword

declares class members or methods as static, so you can access them directly without instantiating the class. Static members (except static methods) cannot be accessed through an object.

In order to be compatible with PHP4, if "visibility" is not 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 using the :: method will result in an E_STRICT level error.

Like all other PHP static variables, static properties can only be initialized to a character value or a constant, and expressions cannot be used. So you can initialize a static property to an integer or array, but it cannot point to another variable or function return value, nor to an object.

After PHP5.3.0, we 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 member code example

<&#63;php
class Foo
{
  public static $my_static = 'foo';
  public function staticValue() {
    return self::$my_static;
  }
}
class Bar extends Foo
{
  public function fooStatic() {
    return parent::$my_static;
  }
}
print Foo::$my_static . " ";
$foo = new Foo();
print $foo->staticValue() . " ";
print $foo->my_static . " ";   // Undefined "Property" my_static 
print $foo::$my_static . " ";
$classname = 'Foo';
print $classname::$my_static . " "; // PHP 5.3.0之后可以动态调用
print Bar::$my_static . " ";
$bar = new Bar();
print $bar->fooStatic() . " ";
&#63;>
Copy after login

Example #2 Static method code example

<&#63;php
class Foo {
  public static function aStaticMethod() {
    // ...
  }
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
&#63;>
Copy after login

A summary of static variables and static methods in static

Static variables

Static variables are variables that only exist in the scope of a function. However, the value of such a variable will not be lost after the function is executed. That is to say, the variable will still remember the original value the next time the function is called. . To define a variable as static, just add the static keyword before the variable.

In a class, the static keyword has two main uses, one is to define static members, and the other is to define static methods. Within a class, you can use the scope qualifier (::) to access variables at different levels of scope.

Static method

There is an important difference between static and non-static methods: when calling a static method, you no longer need to own an instance of the class.

Principles for using static methods and non-static methods: First, if a method does not contain the $this variable, it should be a static method; if you do not need an instance of the class, you may also use a static class, which can eliminate the need for an instance. Chemical work. In addition, the $this variable cannot be used in a static method, because the static method does not belong to a specific instance.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1071223.htmlTechArticleEnable PHP Static keyword tour mode. The static keyword declares class members or methods as static, and no instances are needed. Classification and direct access. Static objects cannot be accessed through an object...
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