This points to the parent class in the inherited class?

Source: Internet
Author: User
 
  M ();    }} Class Child extends pa{public    function m () {        echo ' child\ ' s function ';    }} $obj =new Child (); $obj->run ();


You know what the result is, actually is parents function, however, the M () method is not inherited because Protected,run () is integrated into the subclass, then the this in the method should point to the subclass instance, call the sub-class m method, the result is called, the parent class M method, If private is changed to public, then subclass Method M is called, who can explain


Reply to discussion (solution)

No more detailed explanation of the extends mechanism of the article, the following is just 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 invoked by instantiating the object of the subclass, it actually enters the space of the parent class, then the $ in the parent class space This is scoped to the parent class space itself and cannot be spanned into the subclass's space.

The reason for this speculation is that the reference principle of a PHP variable, that is, when the value of a variable is a reference to another variable, is not the same as the two variables into the same variable, in fact, it is two variables, and destroy one of the time, the other still exists.

So based on this I think that the instantiation object of the subclass should not use the $this in the parent class to represent the object of the subclass, thus causing the object to "leapfrog"

Or that sentence: private is inviolable.

Upstairs reply really incisive Ah!

No more detailed explanation of the extends mechanism of the article, the following is just 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 invoked by instantiating the object of the subclass, it actually enters the space of the parent class, then the $ in the parent class space This is scoped to the parent class space itself and cannot be spanned into the subclass's space.

The reason for this speculation is that the reference principle of a PHP variable, that is, when the value of a variable is a reference to another variable, is not the same as the two variables into the same variable, in fact, it is two variables, and destroy one of the time, the other still exists.

So based on this I think that the instantiation object of the subclass should not use the $this in the parent class to represent the object of the subclass, thus causing the object to "leapfrog"

Public and private results are not the same, when the attribute becomes public,this, it represents the subclass, can you explain it?

If the parent class is class A and the subclass is class B, the individual feels that the memory structure should be

A = Class A

B = {(Inherits from class A) class B} <--$this

When a subclass object is created, the parent class object is created first, and then the memory block of the parent class is used as part of the subclass memory block (subclass = Parent Memory structure + subclass own)

So the method defined in the parent class for this call is either a method of the parent class or overridden by the quilt class in the parent class.

Your subclass instance does not define the Run method and accesses the public method run of the parent class, at which point the $this should already point to the parent class, so $this->m () calls the private m method that is not overridden in the parent class

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

For example here

 
  SD ();  $this point to the parent class, and the SD method is not defined in the parent class, the SD method of the subclass is private, so there is no output and the sub-class SD is changed to public.    }} Class Child extends pa{public    function m () {        $this->a ();    }    Private Function SD () {        echo ' SD ';    }} $obj =new Child (); $obj->m ();


No in-depth understanding of the internal implementation of PHP, Welcome to guide the discussion.

Or that sentence: private is inviolable.

Don't understand, how to explain the difference between public and private results

Private, can only be accessed by themselves. Will not be inherited and overwritten
Public, can be arbitrarily accessed, inherited, overwritten


Or that sentence: private is inviolable.

Don't understand, how to explain the difference between public and private results

Because public and private are 2 different kinds of decorations for PHP

That is, when the parent class is private m, the child class of public m is actually the inner method of the subclass, and the parent class is completely not related, when the call to run, first go back to run declaration of space to find the private method, if found, then directly executed, Otherwise go back to the current object of space to find public or protected method, if not found, and there is extends class will go to class space to find, still not, then will error.


No more detailed explanation of extends mechanism of the article, the following is just my guess

I think the cause of this problem may be that although the parent class is inherited, the parent class actually exists, In other words, if a method in the parent class is invoked by instantiating an object from the subclass, it actually enters the space of the parent class, so the $this in the parent class space is the parent class space itself, and cannot be spanned into the space of the child class.

is so guessing because the reference principle of a variable in PHP, that is, when the value of a variable is a reference to another variable, is not the same as the two variables become the same variable, in fact, it is two variables, and destroy one of the time, the other still exists.

So based on this I think that instantiating objects of subclasses should not use $this in the parent class to represent objects of subclasses, which would cause the object's "leapfrog"

Public and private results to be different when the property becomes public. This represents a subclass, can you explain

as explained above, private is not overwritten, and public can be overwritten, so after you have changed to public, it is the method of the parent class. The quilt class overrides the



Or that sentence: private is inviolable.

Don't understand, how to explain the difference between public and private results

Because public and private are 2 different kinds of decorations for PHP

That is, when the parent class is private m, the child class of public m is actually the inner method of the subclass, and the parent class is completely not related, when the call to run, first go back to run declaration of space to find the private method, if found, then directly executed, Otherwise go back to the current object of space to find public or protected method, if not found, and there is extends class will go to class space to find, still not, then will error.

The run () method is already a subclass of the method, then $child->run () is the subclass of its own method, the run () inside the This also appears in the subclass, refers to the sub-class object, then $this->m () I understand the call because M () of the subclass and not the parent class M (). The subclass and the parent class m () doesn't matter, I've said in my post that M () is not inherited, my question is this point in the run () method, why it is different in private and public, thank you

Private, can only be accessed by themselves. Will not be inherited and overwritten
Public, can be arbitrarily accessed, inherited, overwritten

Irrelevantly Replying



No more detailed explanation of the extends mechanism of the article, the following is just my guess

I think the cause of this problem may be that although the parent class appears to be inherited, However, the parent class is actually present, that is, if a method in the parent class is called through the instantiation object of the subclass, then it actually enters the space of the parent class, so the $this in the parent class space is the parent class space itself, and cannot be spanned into the space of the child class.

is so guessing because the reference principle of a variable in PHP, that is, when the value of a variable is a reference to another variable, is not the same as the two variables become the same variable, in fact, it is two variables, and destroy one of the time, the other still exists.

So based on this I think that instantiating objects of subclasses should not use $this in the parent class to represent objects of subclasses, which would cause the object's "leapfrog"

Public and private results to be different when the property becomes public. This represents a subclass, can you explain

as explained above, private is not overwritten, and public can be overwritten, so you change to public, that is, the method of the parent class the quilt class overrides
and what is the point of this relationship?




or that sentence: private is inviolable

don't understand, how to interpret public and private results differently

because public for PHP and private is 2 different modifiers

that is, when the parent class is private m, the child class's public m is actually just the inner method of the subclass, and the parent class has nothing to do with it, and when the run is called, first go back to the space of the run declaration to find the private method, If found, then directly execute, or go back to the current object of space to find public or protected method, if not found, and extends class will go to class space to find, still not, then will be an error. The

Run () method is already a subclass of method, then $child->run () is the subclass of its own method, the run () inside the This is also in the subclass, refers to the sub-class object, then $this->m () I understand the call because M () of the subclass and not the parent class M (). The subclass and the parent class m () doesn't matter, I've said in my post that M () is not inherited, my question is this point in the run () method, why it is different in private and public, thank you

Run () when is a subclass method, You run is defined in the parent class. Its public means that it can be accessed and rewritten by the quilt class, and does not mean that it is directly subordinate to the subclass of the method ah. The

$child->run () executes when there is no run method in the $child instance. So according to the 8 floor, because child is a subclass, it is not found in the subclass when it is found in the parent class space, and the $this in the Run method in the parent class is the representation of 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 being overwritten, which is defined by the subclass itself, regardless of the parent class. It is called the parent class m, not the child class.

When you change the private of the parent's M () to public, the M () of the parent class has the possibility of being rewritten, so the program will inherit his subclass to look for (that is, your $child), and if it finds the child class, it does not find the M ()

that called the parent class itself.

The first kind looks equivalent to:

 
  M ();    }    Public Function m () {        echo ' child\ ' s function ';    }} $obj =new Child (); $obj->run ();


Is it okay to do that?

Mistake, please ignore. Could not be edited.

$this is the object after the class instantiation
So $this point to an object, not a class.
After instantiation, both the base class and the method inheriting the class are in the object
Specifically, which method is determined by PHP according to the rules

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 ();
Get PA pa::m Parent ' s function
The Pa::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 ();
Get PA child::m child ' s function
The Pa::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 child::m Child ' s function
PA pa::m Parent ' s function

If you can't understand it, you can understand it slowly. Rules don't change because of you.
I would like to "irrelevantly replying" a back!

Or that sentence: private is inviolable.
It's so penetrating.





Or that sentence: private is inviolable.

Don't understand, how to explain the difference between public and private results

Because public and private are 2 different kinds of decorations for PHP

That is, when the parent class is private m, the child class of public m is actually the inner method of the subclass, and the parent class is completely not related, when the call to run, first go back to run declaration of space to find the private method, if found, then directly executed, Otherwise go back to the current object of space to find public or protected method, if not found, and there is extends class will go to class space to find, still not, then will error.

The run () method is already a subclass of the method, then $child->run () is the subclass of its own method, the run () inside the This also appears in the subclass, refers to the sub-class object, then $this->m () I understand the call because M () of the subclass and not the parent class M (). The subclass and the parent class m () doesn't matter, I've said in my post that M () is not inherited, my question is this point in the run () method, why it is different in private and public, thank you

Run () When is the subclass method, you run is defined in the parent class AH. Its public means that it can be accessed and rewritten by the quilt class, and does not mean that it is directly subordinate to the subclass of the method ah.

There is no run method in the $child instance $child->run () execution. So according to the 8 floor, because child is a subclass, it is not found in the subclass when it is found in the parent class space, and the $this in the Run method in the parent class is the representation of 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 being overwritten, which is defined by the subclass itself, regardless of the parent class. It is called the parent class m, not the child class.

When you change the private of the parent's M () to public, the parent's M () is likely to be rewritten, so the program will inherit his subclass to look for (that is, your $child), and if it finds the subclass, it does not find the M () that called the parent class itself.

The $this in the Run method in the parent class is the representation of the parent class itself. By the way, your theory is that the parent class is instantiated when it comes to the power of the sub-class? This can only refer to the object cannot reference the class, you mean to create a subclass, by the way the parent class instantiation? Otherwise this thing is a contradiction, there is no parent class object, which exists this

$this is the instantiated object
Just like $obj =new child (); After the $obj like
If you are accessing the object's methods and properties inside an object, you can't always pass $obj in?
So there's $this this carrier.

In fact, all object-oriented languages have this this, and indeed confused a lot of beginners. There's really nothing to understand.
This is the object itself

$this is the object after the class instantiation
So $this point to an object, not a class.
After instantiation, both the base class and the method inheriting the class are in the object
Specifically, which method is determined by PHP according to the rules

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 ();
Get PA pa::m Parent ' s function
The Pa::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 ();
Get PA child::m child ' s function
The Pa::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 child::m Child ' s function
PA pa::m Parent ' s function

If you can't understand it, you can understand it slowly. Rules don't change because of you.
I would like to "irrelevantly replying" a back!


So, the moderator means that before the subclass is instantiated, the parent class must also be instantiated, otherwise the this in the parent class does not have a parent class object for reference, which is the conclusion of the first piece of code.
But according to the second piece of code, the "This" inside of the parent class run calls the method of the subclass object, so the conclusion is that this points to the subclass, and the first code contradicts

Whether this refers to the parent class or the Child class object
So, is this in the parent class method referring to the parent class or the subclass object? (There is a parent object), according to the PHP code running results, obviously contradictory, one point to the parent class will point to the subclass

$this is the instantiated object
Just like $obj =new child (); After the $obj like
If you are accessing the object's methods and properties inside an object, you can't always pass $obj in?
So there's $this this carrier.

In fact, all object-oriented languages have this this, and indeed confused a lot of beginners. There's really nothing to understand.
This is the object itself

That's what you're saying. I understand that what I really don't understand is that the subclass inherits the parent class method function A () (this method a () is used within this), which inherits this, who is this? 13 floor of the answer to see that he understood what I mean, but the explanation of contradictions, and the moderator still did not understand my meaning, I do not understand the private and this rule, I do not know in the inheritance process, this to the sub-class process, point to change or how to go The running result of your first 2 code is obviously a contradiction point in PHP's running mechanism.

There are no sub-class objects and categorical objects
Also does not exist before the subclass instantiation, the parent class must first instantiate the
Instantiation is just a class
It's just that this class might have inherited methods and properties.

Repeat: This is the object itself.

In this case, the inherited subclass should be equivalent to the following

Class Child extends pa{public    function m () {        echo ' child\ ' s function ';    } Public Function Run () {//Inherits only run (), M () private, not inherited        $this->m ();}    }

So this is the object that points to this class (child), but he uses a non-existent m () method




or that sentence: private is inviolable

don't understand, how to interpret public and private results differently

because public for PHP and private is 2 different modifiers

that is, when the parent class is private m, the child class's public m is actually just the inner method of the subclass, and the parent class has nothing to do with it, and when the run is called, first go back to the space of the run declaration to find the private method, If found, then directly execute, or go back to the current object of space to find public or protected method, if not found, and extends class will go to class space to find, still not, then will be an error. The

Run () method is already a subclass of method, then $child->run () is the subclass of its own method, the run () inside the This is also in the subclass, refers to the sub-class object, then $this->m () I understand the call because M () of the subclass and not the parent class M (). The subclass and the parent class m () doesn't matter, I've said in my post that M () is not inherited, my question is this point in the run () method, why it is different in private and public, thank you


last reply, Don't want to do more tangled up in this question

First, look at the following example

 
  Run ();


And then look.
 
  GetMethods () as $refFun) {echo "Define class Name:", $refFun->getdeclaringclass ()->getname (), "\ n"; echo "Modifier S 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);//From the above output can be seen, $this = $obj,//Here implies an output, why it is hidden, this is the relationship of inheritance, We know that public and protected will quilt the same name to overwrite//But private will be protected, can not be overwritten, then in fact there are 2 parts m, a private m, and the other is the sub-class public m$this->m ();// And the M call here, will find that M call it? It is easy to find the private in the class defined by the current method, because there is no need to consider the issue of inheritance overrides    }protected}class Child extends pa{public    function m () {        echo ' child\ ' s function ';    }} $obj =new Child ();p Rint_method ($obj); $obj->run ();


I hope you can understand.

Looks like you need to do that. Delay static binding

Private, whether or not the parent class cannot be called

$this is the instantiated object
Just like $obj =new child (); After the $obj like
If you are accessing the object's methods and properties inside an object, you can't always pass $obj in?
So there's $this this carrier.

In fact, all object-oriented languages have this this, and indeed confused a lot of beginners. There's really nothing to understand.
This is the object itself
It should be his own! Whether this is a parent class or subclass, or something else?

~ ~ ~ ~ ~ ~ ~ ~ in Learning ~

  • 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.