Home > Backend Development > PHP Tutorial > Static binding and dynamic binding in PHP_PHP tutorial

Static binding and dynamic binding in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:37:54
Original
759 people have browsed it

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 the subclass. The relationship between the function call and the function itself, as well as 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 Utilize any runtime information. It targets function calls and function bodies, or variables and blocks in memory. Because PHP is a dynamic language, it does not use static binding. However, 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, which will be based on this Class itself and not based on access scope.

The actions of 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 a subclass Instances of the subclass will be accessed (instead of accessing members in the parent class).

Look at Figure 1. 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 Produces the same result. Overriding methods operates similarly. In Son, calls to identify are 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 the access restrictions on class members. So change the access method from public It is impossible to proceed as protected.

Dynamic binding


<?php
class Father
​{
​protected $salutation = "Hello there!"; //Greetings
​public function getSalutation()
​{
print("$this->salutation ");
​$this->identify();
}
protected function identify()
​{
print("I am Father. ");
}
};
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
​{
print("I am Son. ");
}
};
​$obj = new Son();
​$obj->getSalutation(); //Output Hey! I am Son.
​?>


//Note: getSalutation() is not overridden in the subclass, but there is still a getSalutation(). $salutation and identify()

in this class

// Dynamically bound to the getSalutation() method in the instance of the Son subclass, so the getSalutation() method of the Son instance is called,

//The member salutation and identify() in the Son class will be called instead of the member salutation and identify() in the parent class.

Private members only exist inside the class they are in. Unlike public and protected members, PHP simulates static binding. See the example in Figure 2. It outputs "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().

Binding and private members

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486526.htmlTechArticleIn addition to restricting access, the access method also determines which method will be called by the subclass or which attribute will be accessed by the subclass. Function call The relationship with the function itself, and the relationship between member access and variable memory address...
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