The reason why PHP class can call the instance method is not reported

Source: Internet
Author: User
Tags php books

Learning PHP is the next day, and I like to study these secrets in depth.

Test with PHP 5.2.5 and PHP 5.4.3 respectively

 
<? Phpclass person {function show (){
Var_dump ($ this); echo "Hello, world";} function _ call ($ name, $ ARGs) {echo "Call yes ";} function _ callstatic ($ name, $ ARGs) {echo "callstatic yes" ;}} person: Show () ;?>

 

1. The class calls the instance method and correctly calls the instance method, because the _ calll and _ callstatic

2. No error

According to the instructions in some PHP books, there should be only class methods that can be called by the class, rather than instance methods of the class, that is, the class can only call static methods ), however, the class actually calls the instance method, which is hard to understand. I found some related information, as follows:

Reference:

-----------------------------------------------------------------

You can use classname: methodname () to directly call common methods in the class at any location of the PHP file. I personally think this is because
The objects stored in the heap area of the memory only store the object attributes, and the object methods are stored in eitherCodeIs the member method of the class. If $ this is not included in the member method of the class called by the object, there is no obvious difference between this method and the static method stored in the static data area. In this case, the object's member method is not significantly different from the object's static method.
Does not change with the calling object.
In addition, the member methods are defined in the class, that is, the relationship between father and son. The difference is that they are born by their wives (static methods ), the other is born with another wife (member method). The son of another wife is called Dad. The private son knows the situation one day (no $ this ). The two will be called Dad.
Now you can use classname: methodname ()

URL: http://segmentfault.com/q/1010000000158671

-----------------------------------------------------------------

Then use 5.4.3 to test the decisive alarm, as shown below:

Another problem involved is:

<? Phpclass person {function show () {var_dump ($ this); echo "Hello, world";} function _ call ($ name, $ ARGs) {echo "Call yes";} function _ callstatic ($ name, $ ARGs) {echo "callstatic yes" ;}} class Child extends person {function tell () {self:: Show () ;}} person: Show (); $ c = new child (); $ C-> tell ();?>

Output:

Null

Object (child) #1 (0 ){}

Hello, world

Note:

1. No object exists for the first time (the reference is correct)

2. This is a child object.

 

Explanation:

In fact, PHP has a calling scope, which seems to call a static method. For example, when $ C is called, it is in the scope of this object, and this scope is being passed, therefore, the above $ this is the object of the Child class. An example is as follows:

 
Class A {function _ construct () {}} Class B extends a {function _ construct () {parent ::__ construct ();}}

We recommend that you do not use a class to call non-static methods.

 

Let's look at the following example:

<? Phpclass person {function _ call ($ name, $ ARGs) {echo "Call yes";} static function _ callstatic ($ name, $ ARGs) {echo "callstatic yes" ;}} class Child extends person {function tell () {self: Show () ;}} person: Show (); $ c = new child (); $ C-> tell ();?>

 

Output:

Callstatic Yes
Call Yes

Note:

As we understand it, when the first person tries to call a static method, it does not call it, that is, execute _ callstatic (); when the $ c Scope object passes the call scope to the parent class, it calls the object method, so it executes _ call ();

A great god also made an explanation, you can look at, URL: http://www.laruence.com/2012/06/14/2628.html

 

 

 

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.