Home > php教程 > php手册 > body text

1.2 Magic methods and delayed static binding

WBOY
Release: 2016-08-23 09:03:38
Original
1132 people have browsed it

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:

class A{<br>}<br>class B extends A{<br>    public static function out(){<br>        return new self();<br>    }<br>}<br>class C extends A{<br>    public static function out(){<br>        return new self();<br>    }<br>}<br>var_dump(B::out());//结果是object(B)#1 (0) { }<br>var_dump(C::out());//结果是object(C)#1 (0) { }<br>然后我们将子类中相同的代码抽取到父类class A中,变成:
Copy after login
class A{<br>    public static function out(){<br>        return new self();<br>    }<br>}<br>class B extends A{<br>}<br>class C extends A{<br>}<br>var_dump(B::out());//结果是object(A)#1 (0) { }<br>var_dump(C::out());//结果是object(A)#1 (0) { }<br>这个结果显然不是我们想要的,这里的问题主要是因为self指代的是它所在的类.这里self在类A里面,所以返回的永远是类A的对象,<br>而我们想要的是让out()方法返回调用它的类的对象而不是它所在的类的对象.怎么办?<br>此时我们马上可以想到$this可以代表调用它的对象,但是out()是一个静态方法,里面是不能出现$this的,怎么办?<br>用static.它也代表调用它的对象如:
Copy after login
class A{<br>    public static function out(){<br>        return new static();<br>    }<br>}<br>class B extends A{<br>}<br>class C extends A{<br>}<br>var_dump(B::out());//结果是object(B)#1 (0) { }<br>var_dump(C::out());//结果是object(C)#1 (0) { }<br>这样就好了.这就是延迟静态绑定.<br><br>在看下面的例子:
Copy after login

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

Copy after login
<br><br>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!