Section 9--Binding_PHP Tutorial

WBOY
Release: 2016-07-21 16:01:03
Original
764 people have browsed it

*
+------------------------------------------------ ----------------------------------+
| = This article is read by Haohappy<>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = Welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/
Section 9--Binding
In addition to restricting access, the access method also determines which method will be subclassed The call 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 data structures, before program execution. 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 is only used during runtime available information. In object-oriented code, dynamic binding means that the decision about which method is called or which property is accessed will be based on the class itself and not based on the access scope.
Public and protected members behave similarly to PHP In previous versions of the function, dynamic binding was used. This meant that if a method accessed a class member that was overridden in a subclass, and was an instance of the subclass, the subclass member would be accessed (while Not accessing members 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 result. Overriding a method operates similarly. In Son, the call to identify is bound to that method.
Even in subclasses the access method is weakened from protected becomes public, 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

Copy code The code is as follows:
class Father
{
protected $salutation = "Hello there!"; //Greetings
public function getSalutation()
{
print("$this->salutationn");
$this->identify(); class Son extends Father
{
protected $salutation = "Hey!"; // protected $salutation in the parent class is overwritten
protected function identify() // protected identify() in the parent class is overwritten
{
                                                                                                                                                                    //Output 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
//are the same as getSalutation() in the instance of the Son subclass ) method is dynamically bound, 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 within the class in which they exist. Unlike public and protected members, PHP simulates static binding. See Example 6.11. It prints "Hello there! I am Father." even though the subclass overrides the salutation. Value. The script binds this->salutation to the current class Father. Similar principles apply to the private method identify().
Listing 6.11 Binding and private members
Copy code The code is as follows:
class Father
{
private $salutation = "Hello there!";
public function getSalutation()
{
print("$this->salutationn");
$this->identify();
} }
private function identify()
                                                                         ("I am Father.
n");
} }
}
class Son extends Father       private function identify()                                                                                                   $obj- >getSalutation(); //Output 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 instance, PHP calls AuthorizedUser::isAuthorized, which will allow deleteUser to execute smoothly.
//haohappy Note: To make it clear in one sentence, it is the object type, method, and attribute binding. Calling an object that exists in both the parent class and the subclass When calling a 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



Copy the code

The code is as follows:
class User
{
protected function isAuthorized() //Whether it is a verified user
return(FALSE); 🎜> }  
public function getName() //Get the name {
return($this->name);
} User
{
                                                                                                         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 you need private members. When does it make sense to use them instead of protected members?
Private members are only available when you don’t want to This is used when subclasses inherit the behavior of a parent class that changes or specializes it. This is less common than you might think. Generally speaking, a good object hierarchy should allow most functionality to be specialized and improved by subclasses. , or change - this is one of the foundations of object-oriented programming. Private methods or variables are needed in certain situations, such as when you are sure that you do not want to allow a subclass to change a specific part of the parent class.



http://www.bkjia.com/PHPjc/316941.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/316941.html

* +---------------------- -------------------------------------------------- -------+ |=This article is for Haohappy reading CorePHP Programming |=Notes from the ClassesandObjects chapter|=Translated mainly + personal...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!