Deep exploration of PHP5.0 object model

Source: Internet
Author: User
In addition to restricted visits, the visit method also determines which method calls the quilt class or which attribute calls the quilt class. the association between function calls and functions and the relationship between member visits and variable memory addresses is called binding. In the computer language, there are two ways to restrict the visit. the Visit method also determines which method will be called by the quilt class or which attribute will be called by the quilt class. the association between function calls and functions and the relationship between member visits and variable memory addresses is called binding.

There are two important binding methods in the computer language: static binding and dynamic binding. Static binding is generated between the data structure and the data structure. before the program is executed, static binding is generated during the compilation period. therefore, no runtime information can be used. It targets function calls and function subjects, or variables and blocks in memory. Because PHP is a dynamic language, it does not apply static binding. However, static binding can be imitated.

Dynamic binding only applies to the visiting requests generated during the runtime and only the available information during the runtime. In object-oriented code, dynamic binding means deciding which method is called or which attribute is visited. it is based on this class rather than the visiting category.

The actions of Public and protected members are similar to those of functions in the first few versions of PHP, and the application is dynamically bound. This means that if a method accesses a class member that is overwritten in the subclass and is an instance of a subclass, the member of the subclass will be visited (rather than visiting members in the parent class ).

Let's take a look at example 6.10. 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 similar results. the control of override methods is similar. In Son, the identify call is bound to that method.

Dynamic binding will still generate even if the access method in the subclass is weakened from protected to public. according to the principle of visiting method application, it is impossible to strengthen the visiting restriction for class members, so it is impossible to change the visiting method from public to protected.

Listing 6.10 Dynamic binding

Class Father
{
Protected $ salutation = 'Hello there! '; // Greetings

Public function getSalutation ()
{
Print ('$ this-> salutationn ');
$ This-> identify ();
}

Protected function identify ()
{
Print ('I am Father.
N ');
}
};

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.
N ');
}
};

$ 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 imitates static binding. See example 6.11. 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 ().

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.