PHP static binding and dynamic binding _php tutorial

Source: Internet
Author: User
In addition to restricting access, the access mode also determines which method will be called by the quilt class or which property will be accessed by the quilt class. The Association of a function call to the function itself, and the relationship between member access and the memory address of a variable, is called a binding.

Are there two main ways to bind in a computer language? Static bindings and dynamic bindings. Static binding occurs between the data structure and the data structure before the program executes. Static bindings occur at compile time and therefore cannot take advantage of any run-time information. It is for function calls with the body of the function, or variables and chunks in memory. Because PHP is a dynamic language, it does not use static bindings. However, you can simulate static bindings.

Dynamic binding is the access request generated for the runtime, using only the information available for the run time. In object-oriented code, dynamic binding means deciding which method is called or which property is accessed, based on the class itself and not on the scope of the access.

The actions of public and protected members are similar to the actions of functions in the 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 a subclass, the members of the subclass are accessed (rather than accessing the members in the parent class).

Look at Figure 1. This code output "hey! I am Son. "Because when PHP calls Getsalutation, it is an instance of son that writes the salutation in father. If salutation is public, PHP will produce the same results. The override method is very similar. In son, the call to identify is bound to that method.

Dynamic binding will still occur even if access in the subclass is weakened from protected to public. In accordance with the principle of access, it is not possible to enhance access restrictions on class members. So it is impossible to change the way of access from public to protected.

Dynamic binding


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 in parent class $salutation overwrite
protected function Identify ()//protected identify () overwrite in parent class
{
Print ("I am Son.");
}
};
$obj = new Son ();
$obj->getsalutation (); Output hey! I am Son.
?>


Note: getsalutation () is not covered in subclasses, but there is still a getsalutation (). $salutation and Identify () in this class

is dynamically bound to the Getsalutation () method in an instance of the son subclass, so call the Getsalutation () method of the instance of Son,

The members in the son class are called Salutation and identify (), not the members of the parent class salutation and identify ().

Private members exist only within the class in which they reside. Unlike public and protected members, PHP emulates static bindings. See example Figure 2. It outputs "Hello there! I am Father. ", although the subclasses overwrite the value of salutation. The script binds this->salutation and the current class father. A similar principle applies to the Private method identify ().

Binding and Private members

http://www.bkjia.com/PHPjc/486526.html www.bkjia.com true http://www.bkjia.com/PHPjc/486526.html techarticle In addition to restricting access, the access mode also determines which method will be called by the quilt class or which property will be accessed by the Quilt class. The function call is associated with the function itself, as well as the member access and variable memory addresses ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.