The invoke _php technique of static static property and statically method in PHP object-oriented

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.