This points to the parent class in the inherited class?

Source: Internet
Author: User
This points to the parent class in the inherited class?
 m();    }}class child extends pa{    public function m(){        echo 'child\'s function';    }}$obj=new child();$obj->run();


You can guess what the result is actually a parents function. However, the m () method is not inherited because protected and run () are integrated into the subclass, this in the method should point to the subclass instance and call the subclass m method, but the result is called. if the m method of the parent class is changed to public, the subclass m method is called, who can explain?


Reply to discussion (solution)

No more detailed explanation of the extends mechanism was found. The following is my guess:

I think the cause of this problem may be that although the parent class is inherited on the surface, the parent class actually exists, that is, if a method in the parent class is called through the instantiated object of the subclass, it actually enters the space of the parent class, in the parent class space, $ this is used by the parent class space, rather than the child class space.

The reason for this conjecture is that the reference principle of the PHP variable, that is, when the value of a variable is referenced by another variable, it is not equivalent that the two variables become the same variable, in fact, it is still two variables, and when one of them is destroyed, the other still exists.

So based on this, I think that the instantiated object of the subclass should not use $ this in the parent class to represent the object of the subclass. this will cause the object to be "out of class"

Or that sentence: private is not an infringement.

The reply upstairs is brilliant!

No more detailed explanation of the extends mechanism was found. The following is my guess:

I think the cause of this problem may be that although the parent class is inherited on the surface, the parent class actually exists, that is, if a method in the parent class is called through the instantiated object of the subclass, it actually enters the space of the parent class, in the parent class space, $ this is used by the parent class space, rather than the child class space.

The reason for this conjecture is that the reference principle of the PHP variable, that is, when the value of a variable is referenced by another variable, it is not equivalent that the two variables become the same variable, in fact, it is still two variables, and when one of them is destroyed, the other still exists.

So based on this, I think that the instantiated object of the subclass should not use $ this in the parent class to represent the object of the subclass. this will cause the object to be "out of class"

The public and private results are different. when the attribute changes to public, this indicates a subclass. can you explain this?

If the parent Class Is Class A and the subclass is Class B, I think the memory structure should be like this.

A = Class

B = {(inherited from Class A) Class B} <-- $ this

When a subclass object is created, the parent class object should be created first, and the memory block of the parent class should be part of the sub-class memory block (subclass = parent class memory structure + subclass self-owned)

Therefore, the "this" method defined in the parent class is either the method of the parent class or the method of overwriting the quilt class in the parent class.

The run method is not defined in your subclass instance, and the common method run of the parent class is accessed. $ this should already point to the parent class, so $ this-> m () A private m method that is not overwritten in the parent class is called.

B = {(inherited from Class A) <-- $ this Class B}

For example, here is an example

 Sd (); // $ this points to the parent class. The sd method of the child class is not defined in the parent class, and the sd method of the child class is private. therefore, no output is provided. The sd method of the child class can be output only when it is changed to public. } Class child extends pa {public function m () {$ this-> a ();} private function sd () {echo 'SD ';}} $ obj = new child (); $ obj-> m ();


I have not thoroughly understood the internal implementation of php. welcome to the discussion.

Or that sentence: private is not an infringement.

I don't understand. how can I explain that the public and private results are different?

Private, which can only be accessed by itself. Not inherited or overwritten
Public, accessible, inherited, and overwritten


Or that sentence: private is not an infringement.

I don't understand. how can I explain that the public and private results are different?

For php, public and private are two different modifiers.

That is to say, when the parent class is private m, the public m of the subclass is actually only the internal method of the subclass, but it has nothing to do with the parent class. when you call run, first, go back to the space declared by run to find the private method. if it is found, run it directly. Otherwise, go back to the space of the current object to find the public or protected method. if not, if an extends class exists, it will be searched for in the class space. if it still does not exist, an error will be reported.


No more detailed explanation of the extends mechanism was found. The following is my guess:

I think the cause of this problem may be that although the parent class is inherited on the surface, the parent class actually exists, that is, if a method in the parent class is called through the instantiated object of the subclass, it actually enters the space of the parent class, in the parent class space, $ this is used by the parent class space, rather than the child class space.

The reason for this conjecture is that the reference principle of the PHP variable, that is, when the value of a variable is referenced by another variable, it is not equivalent that the two variables become the same variable, in fact, it is still two variables, and when one of them is destroyed, the other still exists.

So based on this, I think that the instantiated object of the subclass should not use $ this in the parent class to represent the object of the subclass. this will cause the object to be "out of class"

The public and private results are different. when the attribute changes to public, this indicates a subclass. can you explain this?

As you have explained above, private cannot be overwritten, but public can be overwritten. Therefore, after you change to public, the parent class's method quilt class will overwrite.



Or that sentence: private is not an infringement.

I don't understand. how can I explain that the public and private results are different?

For php, public and private are two different modifiers.

That is to say, when the parent class is private m, the public m of the subclass is actually only the internal method of the subclass, but it has nothing to do with the parent class. when you call run, first, go back to the space declared by run to find the private method. if it is found, run it directly. Otherwise, go back to the space of the current object to find the public or protected method. if not, if an extends class exists, it will be searched for in the class space. if it still does not exist, an error will be reported.

The run () method is already a subclass method, so $ child-> run () executes the subclass method, and this in run () also appears in the subclass, it refers to a subclass object, so $ this-> m () is called because it is m () of the subclass rather than m () of the parent class, which I understand. The subclass has nothing to do with the m () of the parent class. I have already mentioned in my post that m () is not inherited. my problem is that this points to in the run () method, why is the difference between private and public? thank you.

Private, which can only be accessed by itself. Not inherited or overwritten
Public, accessible, inherited, and overwritten

Not answered



No more detailed explanation of the extends mechanism was found. The following is my guess:

I think the cause of this problem may be that although the parent class is inherited on the surface, the parent class actually exists, that is, if a method in the parent class is called through the instantiated object of the subclass, it actually enters the space of the parent class, in the parent class space, $ this is used by the parent class space, rather than the child class space.

The reason for this conjecture is that the reference principle of the PHP variable, that is, when the value of a variable is referenced by another variable, it is not equivalent that the two variables become the same variable, in fact, it is still two variables, and when one of them is destroyed, the other still exists.

So based on this, I think that the instantiated object of the subclass should not use $ this in the parent class to represent the object of the subclass. this will cause the object to be "out of class"

The public and private results are different. when the attribute changes to public, this indicates a subclass. can you explain this?

As you have explained above, private cannot be overwritten, but public can be overwritten. Therefore, after you change to public, the parent class's method quilt class will overwrite.
So what is the relationship with the point of this?




Or that sentence: private is not an infringement.

I don't understand. how can I explain that the public and private results are different?

For php, public and private are two different modifiers.

That is to say, when the parent class is private m, the public m of the subclass is actually only the internal method of the subclass, but it has nothing to do with the parent class. when you call run, first, go back to the space declared by run to find the private method. if it is found, run it directly. Otherwise, go back to the space of the current object to find the public or protected method. if not, if an extends class exists, it will be searched for in the class space. if it still does not exist, an error will be reported.

The run () method is already a subclass method, so $ child-> run () executes the subclass method, and this in run () also appears in the subclass, it refers to a subclass object, so $ this-> m () is called because it is m () of the subclass rather than m () of the parent class, which I understand. The subclass has nothing to do with the m () of the parent class. I have already mentioned in my post that m () is not inherited. my problem is that this points to in the run () method, why is the difference between private and public? thank you.

When is run () A subclass method? run is defined in the parent class. Its public value indicates that it can be accessed and overwritten by the quilt class. it does not mean that it directly belongs to the subclass method.

$ Child-> run (): no run method exists in the $ child instance. Therefore, according to the 8th floor, because child is a subclass, the child class cannot be found in the parent class space. in this case, $ this in the run method of the parent class indicates the parent class itself.

When you use $ this-> m (), because $ this currently represents the parent class, and m () is private, there is no possibility of overwriting, the sub-class defines its own sub-class and has nothing to do with the parent class. The m of the parent class is called, not the subclass.

When you change the m () private of the parent class to public, the m () of the parent class may be overwritten, therefore, the program will inherit from its subclass to find (that is, your $ child). if it finds it, it calls the subclass and does not find the m () that calls the parent class ()

The first one is equivalent:

 m();    }    public function m(){        echo 'child\'s function';    }}$obj=new child();$obj->run();


Can this be done?

Wrong. please ignore it. It cannot be edited ..

$ This is the object after class instantiation.
Therefore, $ this points to an object instead of a class.
After instantiation, methods of both the base class and the inherited class are in the object.
Which method is implemented depends on the rules of php?

class pa{  private function m(){    echo __METHOD__ . ' Parent\'s function';  }  public function run(){    echo __CLASS__ . ' ';    $this->m();  }}class child extends pa{  public function m(){   echo __METHOD__ . ' child\'s function';  }}$obj=new child();$obj->run();
Obtain the pa: m Parent's function.
Pa: The m method is private and cannot be overwritten.

class pa{  public function m(){    echo __METHOD__ . ' Parent\'s function';  }  public function run(){    echo __CLASS__ . ' ';    $this->m();  }}class child extends pa{  public function m(){   echo __METHOD__ . ' child\'s function';  }}$obj=new child();$obj->run();
Obtain the pa child: m child's function.
Pa: The m method is public and can be overwritten.

class pa{  private function m(){    echo __METHOD__ . ' Parent\'s function';  }  public function run(){    echo __CLASS__ . ' ';    $this->m();  }}class child extends pa{  public function m(){   echo __METHOD__ . ' child\'s function';  }  public function run(){    echo __CLASS__ . ' ';    $this->m();    parent::run();  }}$obj=new child();$obj->run();
Get
Child: m child's function
Pa: m Parent's function

If you cannot understand it, just take it easy. The rules won't change because of you.
I would like to "answer your questions" again!

Or that sentence: private is not an infringement.
Brilliant





Or that sentence: private is not an infringement.

I don't understand. how can I explain that the public and private results are different?

For php, public and private are two different modifiers.

That is to say, when the parent class is private m, the public m of the subclass is actually only the internal method of the subclass, but it has nothing to do with the parent class. when you call run, first, go back to the space declared by run to find the private method. if it is found, run it directly. Otherwise, go back to the space of the current object to find the public or protected method. if not, if an extends class exists, it will be searched for in the class space. if it still does not exist, an error will be reported.

The run () method is already a subclass method, so $ child-> run () executes the subclass method, and this in run () also appears in the subclass, it refers to a subclass object, so $ this-> m () is called because it is m () of the subclass rather than m () of the parent class, which I understand. The subclass has nothing to do with the m () of the parent class. I have already mentioned in my post that m () is not inherited. my problem is that this points to in the run () method, why is the difference between private and public? thank you.

When is run () A subclass method? run is defined in the parent class. Its public value indicates that it can be accessed and overwritten by the quilt class. it does not mean that it directly belongs to the subclass method.

$ Child-> run (): no run method exists in the $ child instance. Therefore, according to the 8th floor, because child is a subclass, the child class cannot be found in the parent class space. in this case, $ this in the run method of the parent class indicates the parent class itself.

When you use $ this-> m (), because $ this currently represents the parent class, and m () is private, there is no possibility of overwriting, the sub-class defines its own sub-class and has nothing to do with the parent class. The m of the parent class is called, not the subclass.

When you change the m () private of the parent class to public, the m () of the parent class may be overwritten, therefore, the program will inherit from its subclass to find (that is, your $ child). if it finds it, it calls the subclass and does not find the m () that calls the parent class ()

$ This in the run method of the parent class indicates the parent class itself. In this case, your theory is that the parent class is also instantiated when the strength subclass is used? This can only reference objects and cannot reference classes. do you mean to instantiate the parent class when creating a subclass? Otherwise, this is a conflict. no parent class object exists.

$ This is the instantiated object.
Just like $ obj = new child (); after $ obj
If you access the methods and attributes of an object within the object, you cannot always pass $ obj in?
So we have the $ this carrier.

In fact, this is true for all object-oriented languages, and many beginners are confused. Actually, there is nothing hard to understand.
This is the object itself.

$ This is the object after class instantiation.
Therefore, $ this points to an object instead of a class.
After instantiation, methods of both the base class and the inherited class are in the object.
Which method is implemented depends on the rules of php?

class pa{  private function m(){    echo __METHOD__ . ' Parent\'s function';  }  public function run(){    echo __CLASS__ . ' ';    $this->m();  }}class child extends pa{  public function m(){   echo __METHOD__ . ' child\'s function';  }}$obj=new child();$obj->run();
Obtain the pa: m Parent's function.
Pa: The m method is private and cannot be overwritten.

class pa{  public function m(){    echo __METHOD__ . ' Parent\'s function';  }  public function run(){    echo __CLASS__ . ' ';    $this->m();  }}class child extends pa{  public function m(){   echo __METHOD__ . ' child\'s function';  }}$obj=new child();$obj->run();
Obtain the pa child: m child's function.
Pa: The m method is public and can be overwritten.

class pa{  private function m(){    echo __METHOD__ . ' Parent\'s function';  }  public function run(){    echo __CLASS__ . ' ';    $this->m();  }}class child extends pa{  public function m(){   echo __METHOD__ . ' child\'s function';  }  public function run(){    echo __CLASS__ . ' ';    $this->m();    parent::run();  }}$obj=new child();$obj->run();
Get
Child: m child's function
Pa: m Parent's function

If you cannot understand it, just take it easy. The rules won't change because of you.
I would like to "answer your questions" again!


Therefore, the moderator means that the parent class must be instantiated before the subclass is instantiated. Otherwise, this in the parent class does not have a parent class object for reference. this is the conclusion of the first code.
However, according to the second code, this in the parent class run calls the method of the subclass object. Therefore, this points to the subclass, which is in conflict with the first code.

Whether this points to the parent class or a subclass object
So, is this referenced in the parent class method a parent class or a subclass object? (Is there a parent class object?) according to the running results of php code, there is a clear conflict. point to the parent class and then point to the subclass.

$ This is the instantiated object.
Just like $ obj = new child (); after $ obj
If you access the methods and attributes of an object within the object, you cannot always pass $ obj in?
So we have the $ this carrier.

In fact, this is true for all object-oriented languages, and many beginners are confused. Actually, there is nothing hard to understand.
This is the object itself.

I understand what you said. what I really don't understand is that subclass inherits the parent class method function a () (this is used in this method ), who is this inherited from this? The answer on the 13th floor shows that he understands what I mean, but there are contradictions in the explanations. the moderator still does not understand what I mean. I do not understand the rules of private and this, the city does not know whether this is passed to the subclass during the inheritance process, pointing to the change or what is going on, and the running result of your first two codes, obviously, there are contradictions in the php operating mechanism.

No subclass object or category object
There is no "parent class must also be instantiated before subclass instantiation"
Only one class is instantiated.
However, this class may contain inherited methods and attributes.

Repeat it again: this is the object's own

In this case, the inherited subclass is equivalent

Class child extends pa {public function m () {echo 'child \'s function';} public function run () {// only inherits run (), m () private, not inherited $ this-> m ();}}

So this is the object pointing to this class (child), but he used a non-existent m () method.




Or that sentence: private is not an infringement.

I don't understand. how can I explain that the public and private results are different?

For php, public and private are two different modifiers.

That is to say, when the parent class is private m, the public m of the subclass is actually only the internal method of the subclass, but it has nothing to do with the parent class. when you call run, first, go back to the space declared by run to find the private method. if it is found, run it directly. Otherwise, go back to the space of the current object to find the public or protected method. if not, if an extends class exists, it will be searched for in the class space. if it still does not exist, an error will be reported.

The run () method is already a subclass method, so $ child-> run () executes the subclass method, and this in run () also appears in the subclass, it refers to a subclass object, so $ this-> m () is called because it is m () of the subclass rather than m () of the parent class, which I understand. The subclass has nothing to do with the m () of the parent class. I have already mentioned in my post that m () is not inherited. my problem is that this points to in the run () method, why is the difference between private and public? thank you.


I finally replied to a post and didn't want to worry about this issue.

First, let's look at the example below.

 run();


Next, let's look.
 GetMethods () as $ refFun) {echo "Define class name:", $ refFun-> getDeclaringClass ()-> getName (), "\ n "; echo "Modifiers for method", $ refFun-> name, ": \ n"; echo $ refFun-> getModifiers (). "\ n"; echo implode ('', Reflection: getModifierNames ($ refFun-> getModifiers ())). "\ n" ;}} class pa {private function m () {echo 'parent \'s function';} public function run () {print_method ($ this ); // as shown in the preceding output, $ this = $ obj, // Here is an implicit output. why is it hidden? this is the inheritance relationship. we know that public and protected will be overwritten by the same name as the quilt class. // but private will be protected and cannot be overwritten, in fact, there are two copies of m, one is private m, and the other is the subclass public m $ this-> m (); // while the m here is called, will I find the m call? // Very simple, that is, the private of the class defined by the current method is preferred, because with private, the inheritance overwrite issue does not need to be considered} protected} class child extends pa {public function m () {echo 'child \'s function' ;}}$ obj = new child (); print_method ($ obj); $ obj-> run ();


I hope you can understand.

It seems that you need to delay static binding.

Private, no matter whether it is a parent class or not, cannot be called.

$ This is the instantiated object.
Just like $ obj = new child (); after $ obj
If you access the methods and attributes of an object within the object, you cannot always pass $ obj in?
So we have the $ this carrier.

In fact, this is true for all object-oriented languages, and many beginners are confused. Actually, there is nothing hard to understand.
This is the object itself.
It should be him! Whether this is a parent class or subclass, or another class?

? Burst ~~~ Learning by name ~

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.