PHP static binding and dynamic binding _ PHP Tutorial

Source: Internet
Author: User
PHP static binding and dynamic binding. In addition to restricted access, the access method also determines which method calls the quilt class or which attribute accesses the quilt class. the association between function call and function itself, and the restriction of access between member access and variable memory address also determines which method calls the quilt class or which attribute accesses the quilt class. the association between function calls and functions and the relationship between member access and variable memory addresses is called binding.

Are there two major binding methods in computer languages? Static binding and dynamic binding. static binding occurs between the data structure and the data structure, before the program is executed. static binding occurs during the compilation period, so you cannot use any runtime information. it targets the main body of function calls and functions, or blocks in variables and memory. because PHP is a dynamic language, it does not use static binding. however, static binding can be simulated.

Dynamic binding only uses available information during the runtime for access requests generated during the runtime. in object-oriented code, dynamic binding means deciding which method is called or which attribute is accessed. it is based on the class and not based on the access scope.

The actions of Public and protected members are similar to those of functions in the previous versions of PHP. dynamic binding is used. this means that if a method accesses a class member that is overwritten in the subclass and is a subclass instance, the member of the subclass will be accessed (instead of accessing the member in the parent class ).

See. this code output "Hey! I am Son. "because when PHP calls getSalutation, it is a Son instance that overwrites salutation in Father. if salutation is public, PHP will produce the same result. the operation of override method is similar. in Son, the identify call is bound to that method.

Even if the access method in the subclass is weakened from protected to public, dynamic binding still occurs. according to the principle of access method usage, it is impossible to enhance the access restrictions on class members. therefore, changing the access method from public to protected is impossible.

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 overwritten in the subclass, but there is still a getSalutation (). $ salutation and identify () in this class ()

// It is dynamically bound to the getSalutation () method in the Son subclass instance. Therefore, the getSalutation () method of the Son instance is called,

// Calls the members salutation and identify () in the Son class, instead of the members salutation and identify () in the parent class ().

Private members only exist in their internal classes. Unlike public and protected members, PHP simulates static binding. See the example. it outputs "Hello there! I am Father. ", although the sub-class overwrites the salutation value, the script binds this-> salutation to the current class Father. Similar principles are applied to the private method identify ().

Binding and private members

Callback. Association Between function call and function itself, and between member access and variable memory address...

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.