1. Magic method:
1.__get,__set
__get: Triggered when getting an inaccessible attribute (inaccessible means the attribute does not exist, or there is no access permission)
__set: Triggered when assigning a value to an inaccessible property
2.__isset,__unset
__isset: Triggered when an inaccessible property is determined using the isset() function
__unset: Triggered when the unset() function is used to operate an inaccessible property
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 initializing an object
__destruct: Triggered when the object is destroyed or the script is executed
5.__autoload
__autoload: Triggered when using an inaccessible class
6.__clone
__clone: Triggered when the object is cloned
7.__sleep,__wakeup
__sleep: Triggered when using serialize
__wakeup: Triggered when using unserialize
8.__toString,__invoke
__toString: Triggered when an object is operated as a string. For example, if $obj is an object, echo $obj will trigger __toString
__invoke: Triggered when an object is used as a function. If $obj is an object, $obj() will trigger __invoke
2. Delayed static binding
Put aside concepts and look at examples to understand:
How did delayed static binding appear in the first place? See the example below:
1 |
|
1 |
|
1 |
|
Why is this result? Let’s analyze it:
First, object c calls the get() method, but it is not found in class C, so it looks for it in class B and finds it. Then it executes the get method,
Execute A::foo() first; class A will directly call its own foo(), output `fooA`, and then call out. Obviously the one calling static::out() here is class A, so the output class The name is also A. (here we focus on category A)
Execute parent::foo() again; parent represents the parent class. Here, foo() in class A will be executed, `fooA` will be output, and then static::out() will be executed. At this time, it is not class A that calls this static , but class C, because parent represents the parent class, but does not represent a specific class (here we focus on the methods in the parent class, regardless of who the parent class is).
Then execute self::foo(); self represents the class it is in (class B). It executes foo() first without looking for it in the parent class, so it outputs `fooA` and then executes static::out() ,For the same reason, static is not used here, but class C. Although self represents class B, self cannot represent a specific class.
To put it simply: Object c starts executing get()-->A::foo(); At this time, the chain is broken, and class A directly calls foo(), and object c It doesn’t matter. Of course static here refers to class A.
Next, object c-->parent::foo()--> foo()-->static::out() in class A. To put it bluntly, the parent here is a pointing function, that is, who to execute foo() method. So it can be understood that object c calls the foo method in class A. Then static in foo represents class C
Finally, object c-->self::foo()--> foo()-->static::out() in class A, the same as above, self here also has a pointing function, but in the end We are still at class A. It can be understood as object c calling the foo method in class A. Then static in foo represents class C
1 |
|
1 |
|