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