Home > Article > Backend Development > Section 9 - Binding - Classes and Objects in PHP5 [9]_PHP Tutorial
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. ?>
<?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. ?>
<?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"); ?>