Static binding and dynamic binding

Source: Internet
Author: User
In addition to restricted access, the access method also determines which method calls the quilt class or which attribute accesses the quilt class. the association between function calls and functions and the relationship between member access and variable memory addresses is called binding. are there two major binding methods in computer languages? Static binding and dynamic binding... "> <LINKhref =" http://www.php100.com//statics/style/hea

 

In addition to restricted access, the access method also determines which method calls the quilt class or which attribute accesses the quilt class. the association between function calls and functions and the relationship between member access and variable memory addresses is called binding.

Are there two major binding methods in computer languages? Static binding and dynamic binding. static binding occurs between the data structure and the data structure, before the program is executed. static binding occurs during the compilation period, so you cannot use any runtime information. it targets the main body of function calls and functions, or blocks in variables and memory. because PHP is a dynamic language, it does not use static binding. however, static binding can be simulated.

Dynamic binding only uses available information during the runtime for access requests generated during the runtime. in object-oriented code, dynamic binding means deciding which method is called or which attribute is accessed. It is based on the class and not based on the access scope.

The actions of Public and protected members are similar to those of functions in the previous versions of PHP. Dynamic binding is used. this means that if a method accesses a class member that is overwritten in the subclass and is a subclass instance, the member of the subclass will be accessed (instead of accessing the member in the parent class ).

See. This code output "Hey! I am Son. "because when PHP calls getSalutation, it is a Son instance that overwrites salutation in Father. if salutation is public, PHP will produce the same result. the operation of override method is similar. in Son, the identify call is bound to that method.

Even if the access method in the subclass is weakened from protected to public, dynamic binding still occurs. according to the principle of access method usage, it is impossible to enhance the access restrictions on class members. therefore, changing the access method from public to protected is impossible.

Dynamic binding

<? Php class Father {protected $ salutation = "Hello there! "; // Greeting public function getSalutation () {print (" $ this-> salutation \ n "); $ this-> identify ();} protected function identify () {print ("I am Father. \ n ") ;}}; class Son extends Father {protected $ salutation =" Hey! "; // Protected $ salutation in the parent class is overwritten with protected function identify () // protected identify () in the parent class is overwritten with {print (" I am Son. \ n ") ;}}; $ obj = new Son (); $ obj-> getSalutation (); // output Hey! I am Son.?>

// Note: getSalutation () is not overwritten in the subclass, but there is still a getSalutation (). $ salutation and identify () in this class ()

// It is dynamically bound to the getSalutation () method in the Son subclass instance. Therefore, the getSalutation () method of the Son instance is called,

// Calls the members salutation and identify () in the Son class, instead of the members salutation and identify () in the parent class ().

Private members only exist in their internal classes. Unlike public and protected members, PHP simulates static binding. See the example. It outputs "Hello there! I am Father. ", although the sub-class overwrites the salutation value, the script binds this-> salutation to the current class Father. Similar principles are applied to the private method identify ().

Binding and private members

<? Php class Father {private $ salutation = "Hello there! "; Public function getSalutation () {print (" $ this-> salutation \ n "); $ this-> identify ();} private function identify () {print ("I am Father. \ n ") ;}} class Son extends Father {private $ salutation =" Hey! "; Private function identify () {print (" I am Son. \ n ") ;}}$ obj = new Son (); $ obj-> getSalutation (); // output Hello there! I am Father.?>

The advantage of dynamic binding is to allow the inherited class to change the behavior of the parent class, while retaining the interfaces and functions of the parent class. let's look at the example. because dynamic binding is used, the isAuthorized version called in deleteUser can be determined by the object type. if it is a common user, PHP calls User: isAuthorized and returns FALSE. if it is an AuthorizedUser instance, PHP calls AuthorizedUser: isAuthorized to allow deleteUser to run smoothly.

// Haohappy note: it is clear in one sentence that the object type and method are bound to the property. when you call a method that exists in a parent class and subclass or access an attribute, the system first determines which object type the instance belongs to and then calls the method and attribute in the corresponding class.

Benefits of dynamic binding

<? Php class User // User {protected function isAuthorized () // whether the User is verified {return (FALSE);} public function getName () // Obtain the name {return ($ this-> name);} public function deleteUser ($ username) // delete the user {if (! $ This-> isAuthorized () {print ("You are not authorized. \ n "); return (FALSE);} // delete the user print (" User deleted. \ n ") ;}} class AuthorizedUser extends User // authenticated User {protected function isAuthorized () // override isAuthorized () {return (TRUE );}} $ user = new User; $ admin = new AuthorizedUser; // not authorized $ user-> deleteUser ("Zeev "); // authorized $ admin-> deleteUser ("Zeev");?>

Why do private class members simulate static binding? To answer this question, you need to recall why private members are needed. When can they be used to replace protected members?

Private members are used only when you do not want subclass inheritance to change or specialize in the behavior of the parent class. this situation is less than you think. generally, a good object hierarchy should allow the vast majority of functions to be specialized, improved, or changed? This is one of the foundations of object-oriented programming. Under certain circumstances, private methods or variables are required. For example, when you are sure that you do not want the subclass to change a specific part of the parent class.

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.