Enable the PHPStatic keyword travel mode and static keyword. Enable the PHPStatic keyword tour mode. if the static keyword declares that the class member or method is static, you can directly access it without instantiating the class. You cannot use an object to access the Static mode of enabling the PHP static keyword. the Static keyword
If the declared class member or method is static, you can directly access it without instantiating the class. You cannot access static members through an object (except for static methods ).
To ensure compatibility with PHP4, if "visibility" is not specified, the attribute and method are considered public.
Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods.
Static attributes cannot be accessed by objects through the-> operator.
Use: when a non-static method is called, an E_STRICT error occurs.
Like all other PHP static variables, static attributes can only be initialized as a character value or a constant, and expressions cannot be used. Therefore, you can initialize a static property as an integer or array, but it cannot point to another variable or function return value or to an object.
After PHP5.3.0, we can use a variable to dynamically call the class. However, the value of this variable cannot be self, parent, or static.
Example #1 static member code Example
<? Phpclass 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_st Atic. ""; // you can call print Bar: $ my_static dynamically after PHP 5.3.0. ""; $ bar = new Bar (); print $ bar-> fooStatic (). "";?>
Example #2 static method code Example
<?phpclass Foo { public static function aStaticMethod() { // ... }}Foo::aStaticMethod();$classname = 'Foo';$classname::aStaticMethod(); // As of PHP 5.3.0?>
Summary of static variables and static methods in static
Static variables
Static variables exist only in the function scope. However, after the function is executed, the value of such variables will not be lost. that is to say, when the function is called the next time, the variable will still remember the original value. To define a variable as static, you only need to add the static keyword before the variable.
In a class, the static keyword has two main usage methods: one is to define static members and the other is to define static methods. Within the class, you can use the scope qualifier (: :) to access variables of different levels of scopes.
Static method
There is an important difference between static methods and non-static methods: when calling static methods, you no longer need to own class instances.
Usage principles of static 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 need to use static classes, this removes the need to instantiate classes. In addition, $ this variable cannot be used in static methods, because static methods do not belong to a specific instance.
When you declare a class member or method as Static, you can directly access the class without instantiating the class. A static object cannot be accessed...