Home  >  Article  >  Backend Development  >  Section 9 - Binding - Classes and Objects in PHP5 [9]_PHP Tutorial

Section 9 - Binding - Classes and Objects in PHP5 [9]_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:20:48770browse

Section 9--Binding

In addition to restricting access, the access method also determines which method will be called by the subclass or which property will be accessed by the subclass. The relationship between the function call and the function itself, and The relationship between member access and variable memory address is called binding.

There are two main binding methods in computer languages ​​- static binding and dynamic binding. Static binding occurs between data structures and Between data structures, before the program is executed. Static binding occurs at compile time, so it cannot use any runtime information. It targets function calls and the body of the function, or variables and blocks in memory. Because PHP is a dynamic language , it does not use static binding. But it can simulate static binding.

Dynamic binding is for access requests generated during runtime and only uses available information at runtime. In object-oriented code, dynamic binding Binding means that the decision about which method is called or which property is accessed will be based on the class itself and not on the access scope.

The actions of the public and protected members are similar to the actions of functions in previous versions of PHP , using dynamic binding. This means that if a method accesses a class member that is overridden in a subclass and is an instance of the subclass, the subclass member will be accessed (instead of accessing the member in the parent class).

Look at Example 6.10. This code outputs "Hey! I am Son." Because when PHP calls getSalutation, it is an instance of Son, which overwrites the salutation in Father. If the salutation is public , PHP will produce the same results. The operation of overriding methods is very similar. In Son, the call to identify is bound to that method.

Even if the access method is weakened from protected to public in the subclass, Dynamic binding will still occur. According to the principle of using access methods, it is impossible to enhance access restrictions on class members. Therefore, changing the access method from public to protected is impossible.

Listing 6.10 Dynamic binding Dynamic Binding

<?php 

   class Father 

   { 

       protected $salutation = "Hello there!"; //问候 



       public function getSalutation() 

       { 

           print("$this->salutation
"); 

           $this->identify(); 

       } 



       protected function identify() 

       { 

           print("I am Father.<br>
"); 

       } 

   }; 



   class Son extends Father 

   { 

       protected $salutation = "Hey!";  //父类中的protected $salutation 被覆写 



       protected function identify()    //父类中的protected identify() 被覆写 

       { 

           print("I am Son.<br>
"); 

       } 

   }; 



   $obj = new Son(); 

   $obj->getSalutation();  //输出Hey! I am Son. 

?> 



//Note: getSalutation() is not overridden in the subclass, but there is actually still a getSalutation(). $salutation and identify in this class ()
//Dynamically bound to the getSalutation() method in the instance of the Son subclass, so calling the getSalutation() method of the Son instance,
//will call the member salutation and identify() in the Son class ), instead of the member salutation and identify() in the parent class.

Private members only exist inside the class in which they are located. Unlike public and protected members, PHP simulates static binding. See Example 6.11. It prints "Hello there! I am Father.", even though the subclass overwrites the value of the salutation. The script binds this->salutation to the current class Father. A similar principle applies to the private method identify().

Listing 6.11 Binding and private members
<?php 

   class Father 

   { 

       private $salutation = "Hello there!"; 



       public function getSalutation() 

       { 

           print("$this->salutation
"); 

           $this->identify(); 

       } 



       private function identify() 

       { 

           print("I am Father.<br>
"); 

       } 

   } 



   class Son extends Father 

   { 

       private $salutation = "Hey!"; 

       private function identify() 

       { 

           print("I am Son.<br>
"); 

       } 

   } 



   $obj = new Son(); 

   $obj->getSalutation(); //输出Hello there! I am Father. 

?>



The advantage of dynamic binding is that it allows inherited classes to change the behavior of the parent class while maintaining the interface and functionality of the parent class. See Example 6.12. Due to the use of dynamic binding, the version of isAuthorized called in deleteUser can be determined by the type of the object. If it is an ordinary user, PHP calling User::isAuthorized will return FALSE. If it is an AuthorizedUser For example, PHP calls AuthorizedUser::isAuthorized, which will allow deleteUser to execute smoothly.

//haohappy Note: To put it clearly in one sentence, it is the object type, method, and attribute binding. Calling both a parent class and a subclass When an existing method or accessing an attribute, it will first determine which object type the instance belongs to, and then call the methods and attributes in the corresponding class.

Listing 6.12 Benefits of dynamic binding
<?php 

   class User //用户 

   { 

       protected function isAuthorized()  //是否是验证用户 

       { 

           return(FALSE); 

       } 



       public function getName()  //获得名字 

       { 

           return($this->name); 

       } 



       public function deleteUser($username) //删除用户 

       { 

           if(!$this->isAuthorized()) 

           { 

               print("You are not authorized.<br>
"); 

               return(FALSE); 

           } 



           //delete the user 

           print("User deleted.<br>
"); 

       } 

   } 



   class AuthorizedUser extends User //认证用户 

   { 

       protected function isAuthorized() //覆写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 does it make sense to use them instead of protected members?

Private members are only used when you don’t want the child class to inherit the behavior of changing or specializing the parent class. This situation is rarer than you think. Generally speaking, a good object hierarchy should allow most Most functionality is specialized, improved, or changed by subclasses—this is one of the fundamentals of object-oriented programming. Private methods or variables are necessary in certain situations, such as when you are sure that you do not want to allow a subclass to change a specific function in the parent class. Part.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532554.htmlTechArticleSection 9 - In addition to restricting access, the access method also determines which method will be called by the subclass or which attribute Will be accessed by subclasses. The relationship between function calls and the function itself, as well as member access and variables...
Statement:
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