PHP5 Object Model [9] - Binding

zhaozj2021-02-16  115

Section 9 - Binding In addition to restricting access, the access method also decides which method will be invoked by subclass or which property will be submitted. The function calls and function itself are associated, and the relationship between members accesses the variable memory address, called Binding. There are two main binding methods in the computer language - static binding and dynamic binding. Static binding occurs between data structures and data structures, prior to execution. Static binding occurs during compile, so you can't Utilize any of the running period. It calls the function of the function, or a variable and memory block. Because PHP is a dynamic language, it does not use static bindings. But you can simulate static binding. Dynamic binding For the access request generated by the running period, only the available information of 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 instead of access Range .public and protected members Action is similar to the action of the function in the first few versions of PHP, using dynamic binding. This means that if a method accesses a class member overridden in the subclass, it is a subclass of Examples, members of the subclass will be accessed (rather than accessing members in the parent class). See example 6.10. This code output "Hey! I am Son" because when the PHP call getsalutation, it is an instance of SON, is The Salutation over Father is coming. If the Salutation is public, PHP will produce the same result. The operation of the override method is very similar. In SON, the method is bound to the Identify. Even in the subclass The way is weakened from protected into public. Dynamic binding will still happen. In accordance with the principle of accessing methods, enhanced access restrictions for class members are impossible. So change access from public to protected from PUBLIC to protected. Lloy 6.10 Dynamic Binding dynamic binding

Class Father {Protected $ SALUTATION = "Hello the!"; // Greetings Public Function GetSalutation () {Print ("$ this-> Salutationn"); $ this-> Identify ();} protected function ide1 () {print "I am father.n");}}; class son extends Father {protected $ estutation = "hey!"; // PROTECTED $ SALUTATION in the parent class overwriting protected function Identify () // Parent class Protected Identify () 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 Father Binding. Similar principles Apply to Private Method Identify (). Listing 6.11 Binding and Private Members

Class Father {Private $ SALUTATION = "Hello There!"; 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 Father.?> The benefits of dynamic binding are to allow inheritance classes to change the behavior of the parent class, while maintaining the interfaces and functions of the parent class. See Example 6.12. Using dynamic bindings, ISAUTHORIZED of the deleteuser can be determined by the object's type. 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 perform smoothly .//haohappy Note: Use a sentence to clear, the object type and method, the property binding. Call a parent class and the method of the child or the subclass or access to a property, Judgment instance is the type of object type, then call the method and attributes in the corresponding class. Lloyd 6.12 Dynamic Binding benefits

Class user // user {protected function isauThorized () // is whether it is verifying 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 // user authentication {protected function isAuthorized () // override isAuthorized () {return (TRUE);}} $ user = new user; $ admin = new AuthorizedUser; // not authorized $ user -> Deleteuser ("Zeev"); // Authorized $ admin-> Deleteuser ("Zeev");?>

Why is private class members analog static binding? To answer this question, you need to recall why there is a private member. When do you use them to replace the protected member means? Private members only when you don't want children to inherit or change The behavior of specialty parent class is used. This situation is less than what you imagine. Usually, a good object hierarchical structure should allow most of the function of subclass of subclasses, improve, or change - this is One of the foundations of object-oriented programming. If you need a private method or variable, such as you are sure that you don't want the subclass to change a particular part in the parent class.

转载请注明原文地址:https://www.9cbs.com/read-8135.html

New Post(0)