Section 9 -- bind -- Classes and Objects in PHP5 [9]

Source: Internet
Author: User
Tags bind php programming
/*
+ ------------------------------------------------------------------------------- +
| = Read this article for Haohappy <Core PHP Programming>
| = Notes in the Classes and Objects chapter
| = Translation-oriented + personal experiences
| = Do not repost it to avoid unnecessary troubles. Thank you.
| = Thank you for your criticism and hope to make progress together with all PHP fans!
+ ------------------------------------------------------------------------------- +
*/
Section 9-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 calls and functions and the relationship between member access and variable memory addresses is called binding.
There are two main 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 ).
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 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.
Listing 6.10 Dynamic binding
<? Php
Class Father
{
Protected $ salutation = "Hello there! "; // Greetings
Public function getSalutation ()
{
Print ("$ this-> salutationn ");
$ This-> identify ();
}
Protected function identify ()
{
Print ("I am Father. <br> n ");

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.