PHP Object-oriented static and static methods call, object-oriented static
The examples in this article describe static static properties and static method calls in PHP. Share to everyone for your reference. Specific as follows:
In this paper, we analyze the static and static methods in the object-oriented PHP call. About their invocation (can be called, how to call), need to figure out where they are stored in memory, so it is very easy to understand. Static properties, methods (both static and non-static) in memory, only one location (not a static property, how many objects are instantiated, and how many properties).
Instance:
<?phpheader ("Content-type:text/html;charset=utf-8"); class human{static public $name = "little sister"; public $height = n; sta Tic public Function tell () {echo self:: $name;//static method call static property, use self keyword//echo $this->height;//error. A static method cannot call a non-static property//Because the $this represents the instantiated object, and here is the class, not knowing which object the $this represents} public function say () {echo self:: $name. "I have spoken"; The normal method calls the static property, also uses the Self keyword echo $this->height; }} $p 1 = new Human (); $p 1->say (); $p 1->tell ();//object can access static methods 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 () $this error when no $this is available//But php5.4 above will prompt?>
Conclusion:
(1), static properties do not need to be instantiated to invoke. Because the static property is stored in a class, the calling method is "class name:: Attribute Name";
(2), static methods do not need to be instantiated to invoke. Ditto
(3), static methods cannot invoke non-static properties. Because the non-static attribute needs to be instantiated, it is 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 transforms into a static method;
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/955408.html www.bkjia.com true http://www.bkjia.com/PHPjc/955408.html techarticle PHP Object-oriented calls to static and static methods, object-oriented static This article describes static static properties and static method calls in PHP. Share to everyone for your reference ...