PHP static binding and dynamic binding

Source: Internet
Author: User
PHP static binding and dynamic binding. read about PHP static binding and dynamic binding. in addition to restricting 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. are there two major binding methods in computer languages? Static binding and dynamic binding "> <LINKhref =" http: // www. php100.

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.

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 \ n ");
$ 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 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

<? Php
Class Father
{
Private $ salutation = "Hello there! ";

Public function getSalutation ()
{
Print ("$ this-> salutation \ n ");
$ This-> identify ();
}

Private function identify ()
{
Print ("I am Father. \ n ");
}
}

Class Son extends Father
{
Private $ salutation = "Hey! ";
Private function identify ()
{
Print ("I am Son. \ n ");
}
}

$ Obj = new Son ();
$ Obj-> getSalutation (); // output Hello there! I am Father.
?>

The advantage of dynamic binding is to allow the inherited class to change the behavior of the parent class, while retaining the interfaces and functions of the parent class. let's look at the example. because dynamic binding is used, the isAuthorized version called in deleteUser can be determined by the object type. if it is a common user, PHP calls User: isAuthorized and returns FALSE. if it is an AuthorizedUser instance, PHP calls AuthorizedUser: isAuthorized to allow deleteUser to run smoothly.

// Haohappy note: it is clear in one sentence that the object type and method are bound to the property. when you call a method that exists in a parent class and subclass or access an attribute, the system first determines which object type the instance belongs to and then calls the method and attribute in the corresponding class.

Benefits of dynamic binding

<? Php
Class User // User
{
Protected function isAuthorized () // whether the user is verified
{
Return (FALSE );
}

Public function getName () // Get the name
{
Return ($ this-> name );
}

Public function deleteUser ($ username) // delete a user
{
If (! $ This-> isAuthorized ())
{
Print ("You are not authorized. \ n ");
Return (FALSE );
}

// Delete the user
Print ("User deleted. \ n ");
}
}

Class AuthorizedUser extends User // authenticate the User
{
Protected function isAuthorized () // override isAuthorized ()
{
Return (TRUE );
}
}

$ User = new User;
$ Admin = new AuthorizedUser;

// Not authorized
$ User-> deleteUser ("Zeev ");

// Authorized
$ Admin-> deleteUser ("Zeev ");
?>

Why do private class members simulate static binding? To answer this question, you need to recall why private members are needed. when can they be used to replace protected members?

Private members are used only when you do not want subclass inheritance to change or specialize in the behavior of the parent class. this situation is less than you think. generally, a good object hierarchy should allow the vast majority of functions to be specialized, improved, or changed? This is one of the foundations of object-oriented programming. under certain circumstances, private methods or variables are required. for example, when you are sure that you do not want the subclass to change a specific part of the parent class.

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.