1.2 magic method and latency static binding, 1.2 magic delay static

Source: Internet
Author: User
Tags autoload

1.2 magic method and latency static binding, 1.2 magic delay static

I. Magic methods:

1. _ get ,__ set

_ Get: getInaccessibleTrigger when attributes (Inaccessible indicates that the property does not exist or you do not have the access permission.)

_ Set: triggered when a value is assigned to an inaccessible attribute.

2. _ isset ,__ unset

_ Isset: triggered when the isset () function is used to determine an inaccessible attribute.

_ Unset: triggered when an unset () function is used to operate an inaccessible attribute.

3. _ call ,__ callStatic

_ Call: triggered when an inaccessible method is called.

_ CallStatic: triggered when an inaccessible static method is called.

4. _ construct ,__ destruct

_ Construct: triggered when an object is initialized

_ Destruct: the object is destroyed or triggered when the script is executed.

5. _ autoload

_ Autoload: triggered when an inaccessible class is used

6. _ clone

_ Clone: triggered when the object is cloned

7. _ sleep ,__ wakeup

_ Sleep: triggered when serialize is used

_ Wakeup: triggered when unserialize is used.

8. _ toString ,__ invoke

_ ToString: triggered when an object is operated as a string. For example, $ obj is an object and echo $ obj triggers _ toString.

_ Invoke: triggered when an object is used as a function. For example, $ obj is an object and $ obj () triggers _ invoke.

 

Ii. Delayed static binding

Let's look at the example to understand the concept:

First, how does delayed static binding occur? Take the following example:

Class {
}
Class B extends {
Public static function out (){
Return new self ();
}
}
Class C extends {
Public static function out (){
Return new self ();
}
}
Var_dump (B: out (); // The result is object (B) #1 (0 ){}
Var_dump (C: out (); // The result is object (C) #1 (0 ){}
Then we extract the same code from the subclass to the parent class A and convert it:
Class {
Public static function out (){
Return new self ();
}
}
Class B extends {
}
Class C extends {
}
Var_dump (B: out (); // The result is object (A) #1 (0 ){}
Var_dump (C: out (); // The result is object (A) #1 (0 ){}
This result is obviously not what we want. The problem here is mainly because self refers to the class where it is located. Here self is in Class A, so the returned object is always Class,
What we want is to let the out () method return the object that calls its class instead of the object of its class. What should we do?
At this point, we can immediately think of $ this as the object that calls it, but out () is a static method, which cannot contain $ this. What should we do?
Static. It also indicates the object that calls it, such:
Class {
Public static function out (){
Return new static ();
}
}
Class B extends {
}
Class C extends {
}
Var_dump (B: out (); // The result is object (B) #1 (0 ){}
Var_dump (C: out (); // The result is object (C) #1 (0 ){}
In this case, the static binding is delayed.

Let's look at the example below:

Why is this result? Let's analyze:

First, Object c calls the get () method, but it is not found in Class C, so it goes to class B and finds it. Then execute this get method,

First execute A: foo (); Class A will directly call its own foo (), output 'fooa', and then call out. Obviously, static: out () is called here () class A, so the output class name is. (This focuses on Class)

Run parent: foo (); parent to indicate the parent class. Here, foo () in Class A is executed, 'fooa' is output, and static: out () is executed (), at this time, the static call is not class A, but class C, because parent represents the parent class, but does not represent the specific class (Here we focus on the methods in the parent class, regardless of the parent class ).

Then execute self: foo (); self to indicate its class (Class B). It executes foo () first and does not search for it in the parent class. Therefore, it outputs 'fooa ', then execute static: out (). Similarly, here we use static instead of Class A, but class C. Although self represents Class B, self cannot represent A specific class.

Simply put:The object c starts to execute get () --> A: foo (); then the chain is broken and the Class A directly calls foo, it has nothing to do with Object c. static here, of course, refers to Class.

Next, the object c --> parent: foo () --> static: out () in Class A is A pointing function, that is, the foo () method of the executor. so it can be understood that object c calls the foo method in Class. then static in foo represents Class C

Finally, the object c --> self: foo () --> static: out () in Class A is the same as above. Here, self is also A pointing function, however, Class A is still implemented. it can be understood that object c calls the foo method in Class. then static in foo represents Class C

 

 


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.