Php object-oriented summary $ this $ parentself_PHP tutorial

Source: Internet
Author: User
Php object-oriented summary $ this $ parentself. Php object-oriented summary $ this $ parentself although it takes a long time to get started with php, sometimes it may be a bit uncertain and confused when using some basic things. Simple summary of object-oriented php object-oriented $ this $ parent self
Although php has been exposed for a long time, sometimes some basic things may be uncertain and confused. Object-oriented involves a lot of things. Let's summarize the php attributes, objects, and usage scenarios of the access method $ this $ parent self.

1. PHP class attribute definition and access method:

1

2 class testClass {

3 const tConst = 1;

4 public $ tVar = 2; // or public $ tVar; a variable modifier is required.

5 static $ tSta = 3;

6

7 public function _ construct (){

8 echo $ this-> tConst; // no error, no output

9 echo self: tConst; // correct output 1

10

11 echo $ this-> tVar; // note that the $ symbol is not available before tVar.

12 echo self: $ tVar; // Access to undeclared static property: testClass: $ tVar

13 echo self: tVar; // add the $ symbol before tVar, Undefined class constant 'tvar'

14

15 echo $ this-> tSta; // no error, no output

16 echo self: $ tSta; // output 3 correctly

17

18}

19}

20

21 $ otestClass = new testClass ();

Summary:

$ OtestClass = new testClass () when instantiating an object; () in testClass () can be omitted. when a constructor explicitly declares a parameter, you need to input a parameter here.

If the referenced variable or method is declared as const (defining constant) or static (declaring static), the following operators must be used ::

If the referenced variable or method is not declared as const or static, you must use the operator->

Access the const or static variable or method from the inside of the class and use the self

The internal access from the class is not a const or static variable or method. use the self-referenced $ this

2. Where does $ this-> actually point?

1 class testClass {

2 var $ nick = '';

3

4 public function _ construct ($ nick ){

5 $ this-> nick = $ nick;

6}

7

8 public function display (){

9 echo $ this-> nick;

10}

11}

12

13 $ otestClass1 = new testClass ('Frank ');

14 $ otestClass1-> display (); // echo $ otestClass1-> nick; same as the result

In the above example, $ this-> display (); is actually $ otestClass1-> nick, so $ this is determined by the instantiated object, pointer to the current object instance. This applies to variables and methods.

$ This-> method () example:

1 class baseClass {

2 public function testFunc (){

3 echo "\ n". 'I'm boss ';

4}

5}

6

7 class parentClass extends baseClass {

8 public function testFunc (){

9 echo "\ n". 'I am the Up ';

10}

11}

12

13 class testClass extends parentClass {

14 var $ nick = '';

15

16 public function _ construct ($ nick ){

17 $ this-> nick = $ nick;

18}

19

20 public function display (){

21 echo $ this-> nick;

22 $ this-> testFunc ();

23}

24}

25

26 $ otestClass1 = new testClass ('Frank ');

27 $ otestClass1-> display ();

What is the final output result of such code? The key is to look at the testFunc () method.

If $ this is used in the class to call a method or variable that does not exist in the current class, it searches for the parent class in sequence until it cannot be found and returns an error.

Based on the first one, if the required method or variable is found, the search will stop. if the parent class has the same content, select

So the above code output is:

Frank

I'm the up

3. about parent ::

Parent is a reference to the parent class.

1

2 class {

3 public $ a = 'a ';

4 public function callFunc (){

5 echo 'parent ';

6}

7}

8

9 class B extends {

10 public function _ construct (){

11 parent: callFunc ();

12 echo "\ n". $ this->;

13}

14}

15

16 $ oB = new B;

For example, in the above code, if you want to call the callFunc () method in the parent class A in class B, you need to use parent: callFunc (), however, this method must be public-modified in Class A. Otherwise, the system will prompt Fatal error: Call to private method A: callfunc () from context 'B '...

In addition, for the attribute $ a in the parent class, $ this is used during the call, and the access fails with parent. If $ a is defined as public static $ A in Class a, parent: $ a is required for access.

Note: the parent does not have the passed Property. it can only represent the parent class of the current class. In this example, Class A inherits the base class and defines the baseFunc () method in the base class, therefore, using parent: baseFunc () in class B is incorrect and can only be used in class.

4. self: where does it point again?

Simply put, self is irrelevant to a specific instance. it only points to the current class and is not affected by a specific class object.

1 class testClass {

2 public static $ count = 0;

3 public $ num = 0;

4

5 function _ construct (){

6 self: $ count ++;

7 $ this-> num ++;

8}

9

10 function display (){

11 echo self: $ count. "\ n ";

12 echo $ this-> num. "\ n ";

13}

14}

15

16 $ oTest1 = new testClass;

17 $ oTest1-> display (); // output: 1 1

18

19 $ oTest2 = new testClass;

20 $ oTest2-> display (); // output: 2 1

In the above example, self: $ cout always points to the class itself, and $ num exists independently in each specific instance.

5. conclusion:

$ This points to the current instance

$ Parent is a reference of the parent class

Self refers to a reference to the current class.

Although using $ this $ parent self has been in contact with php for a long time, sometimes it may be a bit uncertain and confused when using some basic things. Object-oriented...

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.