Home > Article > Backend Development > Section 9 Binding [9]_PHP Tutorial
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, as well as the relationship between member access and the 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 the program is executed. Static binding occurs at compile time, so it cannot be used Any runtime information. It targets function calls and function bodies, or variables and memory blocks. Because PHP is a dynamic language, it does not use static binding. But static binding can be simulated.
Dynamic binding is for access requests generated during runtime, using only the information available at runtime. In object-oriented code, dynamic binding means deciding which method is called or which property is accessed will be based on this class itself rather than based on access scope.
Public and protected members act similarly to functions in previous versions of PHP, using dynamic binding. This means that if a method accesses a method that is overridden in a subclass The class member is written and is an instance of a subclass. The members of the subclass will be accessed (instead of accessing the members of the parent class).
See 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 salutation is public, PHP will produce the same result. The operation of the overriding method 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. In accordance with the principle of using access methods, enhance the access to class members Access restriction is impossible. So changing the access method from public to protected is impossible.
Listing 6.10 Dynamic binding Dynamic binding
class Father
{
protected $salutation = "Hello there!"; file://greetings
public function getSalutation()
{
print ("$this->salutationn");
$this->identify();
}
protected function identify()
{
print("I am Father.
n");
}
};
class Son extends Father
{
protected $salutation = "Hey!"; file://parent protected $salutation in the class is overwritten
protected function identify() file://protected identify() in the parent class is overwritten
{
print("I am Son.
}
};
$obj = new Son();
$obj->getSalutation(); file://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 are located. Unlike public and protected members, PHP simulates static binding. See Example 6.11. It prints "Hello there! I am Father.", even though subclasses override it. The value of the salutation. 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
class Father
{
private $salutation = "Hello there!";
public function getSalutation()
{
print("$this- >salutationn");
$this->identify();
}
private function identify()
{
print("I am Father.
}
}
class Son extends Father
{
private $salutation = "Hey!";
private function identify()
{
print("I am Son.
n");
}
}
$obj = new Son();
$obj->getSalutation (); file:// 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 instance of AuthorizedUser, PHP Calling AuthorizedUser::isAuthorized will allow deleteUser to execute smoothly.
//haohappy Note: To put it clearly in one sentence, it is the object type, method, and attribute binding. Call a method that exists in both the parent class and the subclass. Or when 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
class User file://user
{
protected function isAuthorized() file://whether it is an authenticated user
{
return(FALSE);
}
public function getName() file://Get name
{
return($this->name);
}
public function deleteUser($username ) file://delete user
{
if(!$this->isAuthorized())
{
print("You are not authorized.
n");
return(FALSE);
}
//delete the user
print("User deleted.
n");
}
}
class AuthorizedUser extends User file:// Authenticated user
{
protected function isAuthorized() file://overwrite 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 and when to use them instead Do protected members make sense?
Private members are only used when you don’t want the subclass to inherit to change or specialize the behavior of the parent class. This situation is rarer than you think. Generally speaking, A good object hierarchy should allow most functionality to be specialized, improved, or changed by subclasses—this is one of the fundamentals of object-oriented programming. There are certain situations where private methods or variables are necessary, such as when you are sure you don't want to allow A subclass changes a specific part of the parent class.