A practical method for phpStatic keywords. 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 compatible with PHP4 in static methods. if "visibility" is not specified, attributes and methods are regarded as public.
Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods.
Static attributes can also 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.
The code is as follows:
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. "\ n ";
$ Foo = new Foo ();
Print $ foo-> staticValue (). "\ n ";
Print $ foo-> my_static. "\ n"; // Undefined "Property" my_static
Print $ foo: $ my_static. "\ n ";
$ Classname = 'foo ';
Print $ classname: $ my_static. "\ n"; // can be dynamically called after PHP 5.3.0
Print Bar: $ my_static. "\ n ";
$ Bar = new Bar ();
Print $ bar-> fooStatic (). "\ n ";
?>
Use the Static keyword in PHP to define Static attributes and methods.
Example 1: Static attribute reference method
The code is as follows:
/*
* Author: ajax123
* Qq: 283400245
*/
Class person {
Static $ name = "ajax123"; // declare static attributes
Static $ age = 25; // declare static attributes
Static $ address = "Beijing"; // declare static attributes
Function song (){
Echo "My name is:". self: $ name ."
"; // Inside the class: access static attributes through the self class
Echo "I am". self: $ age ."
"; // Inside the class: access static attributes through the self class
Echo "I live in". self: $ address ."
"; // Inside the class: access static attributes through the self class
}
}
Echoperson: $ name ."
"; // Class external: access static attributes through class name person
Echoperson: $ age ."
"; // Class external: access static attributes through class name person
Echoperson: $ address ."
"; // Class external: access static attributes through class name person
?>
Example 2: static method reference
The code is as follows:
/*
* Author: ajax123
* Qq: 283400245
*/
Class person {
Static $ name = "ajax123"; // declare static attributes
Static $ age = 25; // declare static attributes
Static $ address = "Beijing"; // declare static attributes
Staticfunction song () {// declare static method song
Echo "My name is:". self: $ name ."
"; // Inside the class: access static attributes through the self class
Echo "I am". self: $ age ."
"; // Inside the class: access static attributes through the self class
Echo "I live in". self: $ address ."
"; // Inside the class: access static attributes through the self class
}
}
Person: song ()."
"; // Class external: access static methods by class name person
?>
Bytes. Because static methods can be called without passing through objects, the pseudo variable $ this does not exist in static methods...