The examples in this article describe static static properties and calls to static methods in PHP. Share to everyone for your reference. Specifically as follows:
In this paper, we analyze the static static properties of the PHP object-oriented system and the call to it. It is easy to understand how they are called (whether they can be invoked or how they are invoked), and they need to understand where they are stored in memory. Static properties, methods (both static and non-static) in memory, only one location (not static properties, how many instantiated objects, there are many attributes).
Instance:
<?php
Header ("Content-type:text/html;charset=utf-8");
Class human{
static public $name = "little sister";
Public $height = 180;
static public function tell () {
echo self:: $name;//static method calls the//echo, using the Self keyword
$this->height;//error. Static methods cannot call Non-static properties
//Because the $this represents the instantiated object, and this is the class, and does not know which object the $this represents
} public
function say () {
echo self:: $name. "I spoke";
The normal method calls the static property, also using the Self keyword
echo $this->height;
}
}
$p 1 = new Human ();
$p 1->say ();
$p 1->tell ();//object can access static method
echo $p 1:: $name;//object access static property. You cannot access $p1->name
///Because the memory location of the static property is not in the object
Human::say ()//wrong. Say () method has $this error, can produce results without $this
//But php5.4 above will prompt
?>
Conclusion:
(1), static properties can be invoked without instantiation. Because the position of the static property is in the class, the method is called "Class Name:: Property name";
(2), static methods can be invoked without instantiation. Ditto
(3), static methods cannot invoke Non-static properties. Because non-static attributes need to be instantiated, they are stored in the object.
(4), static methods can call Non-static methods, using the Self keyword. In PHP, a method is self:: After that, it automatically turns into a static method;
I hope this article will help you with your PHP program design.