Differences between self, parent, and this in phpclass and instance introduction

Source: Internet
Author: User
I am confused with public, private, protected, and this and self. I have already written a blog post about public, private, and protected. The following describes how to use this, self, and parent.

1. to use this, you must be in a situation like this. Otherwise, it will report an error: Fatal error: Using $ this when not in object context.
2. this can call methods and attributes in this class, or call methods and attributes that can be called in the parent class.

II. self

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


3. parent

1. parent can access static attributes and static methods in the parent class.
2. when parent is used, you do not need to instantiate

 '; Public $ tank = 'zhangying
'; Public function _ construct () {$ this-> public = 'public
'; $ This-> private = 'private
'; $ This-> protected = 'protected
';} Public function tank () {// private methods cannot be inherited. they are changed to public and protected if (! Isset (self ::$ instance [get_class ()]) {$ c = get_class (); self ::$ instance = new $ c;} return self :: $ instance;} public function pub_function () {echo "you request public function
"; Echo $ this-> public;} protected function pro_function () {echo" you request protected function
"; Echo $ this-> protected;} private function pri_function () {echo" you request private function
"; Echo $ this-> private;} static function sta_function () {echo" you request static function
";}} Class test1 extends test {static $ love =" tank
"; Private $ aaaaaaa =" ying
"; Public function _ construct () {parent: tank (); parent: :__ construct ();} public function tank () {echo $ this-> public; echo $ this-> protected; echo $ this-> aaaaaaa; $ this-> pro_function ();} public function testbench function () {echo self: $ love; echo self :: $ 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_function (); self: pro_function (); echo "you request extends_private function
";}} Error_reporting (E_ALL); $ test = new test1 (); $ test-> tank (); // The subclass and parent class have attributes and methods with the same name, when instantiating a subclass, the method in the subclass is called. Test1: test1_function (); test1: extends_function (); // after executing a part, the following error occurs: Using $ this when not in object context in D: \ xampp \ htdocs \ mytest \ www4.php on line 32?>

1. when we call $ test-> tank ();, $ this in tank is a pair of images, which can call this class, methods and attributes in the parent class,

2, test1: test1_function (); when we call a Non-static method using a static method, a warning is displayed. Non-static method test: test1_function () shocould not be called statically, we can see no. self can call the static attribute in the parent class. parent can call the static attribute in the parent class. an error is returned when the two call non-static attributes. Comments in the code

3, test1: extends_function (); in this step, an error is reported, and $ this is used in non-peer images. Why is this? test1: extends_function (); only calls a method in class and does not instantiate it. so there is no such object. when $ this is used in the parent class, an error is reported.

For more information about the differences between self, parent, and this in php class and related articles about the instance introduction, please refer to PHP!

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.