Ninth - Binding - Classes and Objects in PHP5 [9]
Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ ------------------------------------------ | = This article is HaohappY << CORE PHP Programming >> | = Classes and Objects Chapter Note | = Translate Main Personal Experience | = To avoid unnecessary troubles, please do not reprint, thank you | = Welcome criticism, hope and all PHP enthusiasts progress together! ----------------------------------------- ---------------------------------- * / Section 9 - Bindings In addition to restrictions, The access method also decides which method will be invoked by subclass or which attribute will be submitted. The function call and the function itself are associated, and the relationship between members accesses and variable memory addresses, called binding. There are two main components in computer language. Binding mode - static binding and dynamic binding. Static binding occurs between data structures and data structures, prior to execution. Static binding occurs during the compile period, so any running period cannot be utilized. It calls the function call With the main body of the function, the variables and blocks in memory. Because PHP is a dynamic language, it does not use static binding. But you can simulate static binding. Dynamic binding, the access request generated by the running period, only To the running period, in the object-oriented code, dynamic binding means that which method is called or which attribute is accessed, which will be based on this class itself without access .public and protected members are similar to PHP The action of the function in the first few versions, using dynamic binding. This means if a method accesses a class member overridden in the subclass, and is a subclass of instances, the members of the subclass will be accessed (while Not a member of the parent class). Look at the 6.10. This code output "Hey! I am Son" because the PHP call getSalutation is a SON instance, which is written in the Father. If Salutation It is public, PHP will produce the same result. The operation of overwriting method is very similar. In Son, the method is bound to the Identify call. Even if the access method is weakened from protected into public, dynamic binding It will still happen. In accordance with the principle of accessing methods, enhancement is impossible for access restrictions for class members. So accessing Ways from PUBLIC to protected into protected. Lloy 6.10 Dynamic Binding Dynamic Binding Php class father {protected $ salutation = "hello there!"; // Greetings public function getSalutation () {print ("$ this-> salutationn "); $ this-> identify ();} protected function Identify () {print (" I am father.
n ");}}; Class Son Extends Father {Protected $ SALUTATION =" Hey! "; / / Parent class Protected $ Salutation overwrites protected function Identify () // The protected Identify () in the parent class is overwritten {print ("I am Son.
n");}}; $ obj = new Son (); $ obj-> getSalutation (); // Output hey! i am Son.?>
// Note: There is no getsalutation () in the subclass, but there is still a getSalutation (). Dynamic () method in this class is a getSalutation () method in the instance of the SON subclass Bind, so the getSalutation () method of the instance of SON is called, // will call the member Salutation and Identify () in the SON class, not the member Salutation and Identify () in the parent class (). Private members only exist in their Interior. Not like members of public and protected, PHP simulates static binding. See Example 6.11. It outputs "Hello There! I am Father.", Although the subclass overrides the value of the Salutation. Script will this-> Salutation and Current class Father binding. Similar principles Apply to Private Method Identify (). Listing 6.11 Binding and Private Members Php class father {private $ estutation = "Hello theate!"; Public function getsalutation () {print ("$ this -> Salutationn "); $ this-> Identify ();} private function Identify () {print (" I am father.
n ");}} Class Son Extends Father {Private $ SALUTATION =" Hey! " private function ide1 () {print ("I am Son.
N");}}} $ obj = new Son (); $ obj-> getsalutation (); // Output Hello there! I am faather.? > Dynamic binding is to allow inheritance classes to change the behavior of the parent class, while maintaining the interface and function of the parent class. See Example 6.12. Due to the use of dynamic binding, the ISAUTHORIZED of the applicationUser can be The type is determined. If it is a normal user, the PHP call user :: isauThorized will return false. If it is an instance of Authorizeduser , PHP call authorizeduser :: isauthorized, will allow deleteuser to be executed smoothly .//haohappy Note: Clear with a sentence is the object type and method, attribute binding. Call a parent class and subclass or access a property or access an attribute When you first judge which object type belongs to the instance, then call the method and properties in the corresponding class. Lloy 6.12 Dynamic Binding benefits Php class user // User {protected function isauthorized () // is a verification user {Return (false);} public function getname () // Gets the name {RETURN ($ this-> name);} public function deleteuser ($ username) // Delete user {if (! $ this-> isauThorized ()) {Print ("You are not authorized.
N"); return (false);} // delete the user print ("user deleted.
n");}}} Class AuthorizedUser Extends User // Certified User {protected function isauthorized () // overwritten isauthorized () {return (true);}} $ user = new user;