Static binding later

Source: Internet
Author: User
About the late static binding to see the php Manual on the Late static binding, still do not understand, the original address http://cn2.php.net/manual/zh/language.oop5.late-static-bindings.php

The fourth example
 

The result is:
A
C
C
When calling parent: foo () and self: foo (), why does it call who () of Class C ()?
Which of you can explain? thank you.


Reply to discussion (solution)

Explanation of the static operator:
Later static binding attempts to bypass the restriction by introducing a keyword to indicate the class initially called during the runtime.
The class originally called is C.

A: foo (); the method of Class A is called, which you understand
Self: foo (); // The self is actually a C class. Do you understand? C: test () C inherits the test () method of B.
Parent: foo (); // because static: who (); instead of self: who (). The current class called by this method, that is, the foo () method of class C.

Maybe you still don't understand why class A didn't call C's who () method. this is due to the special relationship of parent. Delayed static binding is designed to solve the problem of inheritance methods between child classes and parent classes.

You do not have to remember this concept or scenario. when you encounter a static function inheritance and overwrite problem and find that it is not what you think, you can add static and try again, OK.

I personally feel that the features of object-oriented language are more personalized, while the basic things are nothing more than encapsulation, polymorphism and inheritance. more abnormal features are generally not required, when you need it, you must also consider whether it is reliable and trustworthy.

Isn't the manual clear?
------------------------------------------------------
"Post-binding" means that static: is not resolved to define the class of the current method, but calculated during actual operation. It can also be called "static binding" because it can be used for (but not limited to) calling static methods.
-------------------------------------------------------

#1 there is a small problem

Self: foo (); // The self is actually a C class. Do you understand? C: test () C inherits the test () method of B.


Inaccurate. self is still class B, but it does not overwrite the foo method. Therefore, the foo method of the parent class A is called.
If self is actually a class C, try to change self: foo (); to self: who ();. print C, but print B, this is exactly the difference between self and static.

Thank you for your correction. I have learned the manual. isn't it clear?
------------------------------------------------------
"Post-binding" means that static: is not resolved to define the class of the current method, but calculated during actual operation. It can also be called "static binding" because it can be used for (but not limited to) calling static methods.
----------------------------------------------......

A: foo (); // A indicates Class A. access the foo and who methods of Class.
Parent: foo (); // call the parent class of Class B ?? The foo method of A and the original caller of the foo method is C.
Self: foo (); // self refers to the class that defines the method, that is, B, but B does not define the foo method. it passes the original caller C up,
// Access the foo method of the parent class, and finally access the who method of c;


So this answers the question on the third floor: if self: foo (); is changed to self: who, because self refers to B, and B has the who method, so the result is B.

Static call uses parent: or self: To forward original call information.

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.