What is the difference between self,parent,this in PHP class?

Source: Internet
Author: User
Tags php class
One, this

1, to use this, you will have a situation of the image, otherwise it will error, Fatal error:using $this when the not in object context.
2,this can call methods and properties in this class, or can call the adjustable methods and properties in the parent class

Two, self

1,self can access static properties and static methods in this class, and can access static and static methods in the parent class.
2, when using self, you can not instantiate the


Third, parent

1,parent can access static properties and static methods in the parent class.
2, when using the parent, you can not instantiate the

The code is as follows:

<?phpclass test{public $public; private $private; protected $protected; static $instance; static $good = ' Tankzhang &l T;br> '; Public $tank = ' zhangying <br> '; Public Function construct () {$this->public = ' public <br> '; $this->private = ' private <br> ' ; $this->protected = ' protected <br> ';  The Public Function tank () {//Private method cannot be inherited, and replaced with public,protected if (!isset (self:: $instance [Get_class ()])) {$c = Get_class (); self:: $instance = new $c;} Return to self:: $instance; } public Function Pub_function () {echo "Your request public function<br>"; Echo $this->public;} protected Fu Nction pro_function () {echo "You request protected function<br>"; Echo $this->protected;} private Function Pri_f Unction () {echo "You request private function<br>"; Echo $this->private;} static function Sta_function () {echo] You request the static function<br> "; }}class Test1 extends test{static $love = "Tank <br> "; Private $aaaaaaa = "Ying <br>"; Public Function construct () {Parent::tank (), Parent::construct (),} public function tank () {echo $this->public; echo $t his->protected; Echo $this->aaaaaaa; $this->pro_function ();   } public Function Test1_function () {echo-Self:: $love, echo Self:: $good; Echo Parent:: $good; Echo Parent:: $tank;     Fatal error:access to undeclared static property:test:: $tank echo Self:: $tank; Fatal error:access to undeclared static property:test:: $tank} static function Extends_function () {Parent::sta_functio N (); Self::p ro_function (); echo "You request Extends_private function<br>";            }}error_reporting (e_all); $test = new Test1 (); $test->tank (); Subclasses and parent classes have properties and methods of the same name, and methods in subclasses are called when subclasses are instantiated.  Test1::test1_function (); Test1::extends_function (); After performing part of the report fatal error:using $this when the not in object context in D:\xampp\htdocs\mytest\www4.php on line 32?>

1, when we call $test->tank (); This method, tank inside the $this is a pair of like, this pair of like can call the class, the parent class of methods and properties,

2,test1::test1_function (); When we use a static method to invoke a non-static method, the warning is displayed, Non-static method Test::test1_function () should not is called statically can be seen, self can call this class, A static property in the parent class that can invoke a static property in the parent class, and the call to a non-static property causes an error. There are comments in the code

3,test1::extends_function (); This step will error, reported in the non-image using $this. Why is this, test1::extends_function (); Just call a method in class, and there is no instantiation, so there is no image at all, and when $this is used in the parent class, it will be an error.

Related Article

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.