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.
Copy codeThe Code is as follows:
<? 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. "\ 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
Copy codeThe Code is as follows:
<? Php
/*
* 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. "<br>"; // class: Access static attributes through the self class
Echo "I am". self: $ age. "<br>"; // class: Access static attributes through the self class
Echo "I live in". self: $ address. "<br>"; // class: Access static attributes through the self class
}
}
Echoperson: $ name. "<br>"; // class exterior: Access static attributes by class name person
Echoperson: $ age. "<br>"; // class exterior: Access static attributes by class name person
Echoperson: $ address. "<br>"; // class exterior: Access static attributes by class name person
?>
Example 2: static method reference
Copy codeThe Code is as follows:
<? Php
/*
* 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. "<br>"; // class: Access static attributes through the self class
Echo "I am". self: $ age. "<br>"; // class: Access static attributes through the self class
Echo "I live in". self: $ address. "<br>"; // class: Access static attributes through the self class
}
}
Person: song (). "<br>"; // class External: Access static methods by class name person
?>