PHP 5.0 Object Model Deep Exploration 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.

There are two main ways of binding in computer languages-static binding and dynamic binding. 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).

See example 6.10. 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 to the operation. 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 impossible to enhance access restrictions on class members, so changing the access mode from public to protected is not possible.

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 in parent class $salutation overwrite

protected function Identify ()//protected identify () overwrite in parent class
{
Print ("I am Son.")
n ");
}
};

$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 6.11. It outputs "Hello there! I am Father. ", although the subclasses overwrite the salutation value, the script this->salutation and the current class Father bound. A similar principle applies to the Private method identify ().

Listing 6.11 Binding and private members

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

Public Function getsalutation ()
{
Print ("$this->salutationn");
$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 benefit of dynamic binding is that it allows inheriting classes to change the behavior of the parent class, while preserving the interface and functionality of the parent class, see Example 6.12. Because of the use of dynamic binding, version of the isauthorized called in DeleteUser can be determined by the type of the object. If it is a normal user,php call, user::isauthorized will return false. If it is an instance of Authorizeduser, PHP calls Authorizeduser::isauthorized, will allow DeleteUser to execute smoothly.

Haohappy Note: In a word clear, is the object type and method, property binding. When you call a method that exists in a parent class and a subclass, or access a property, you first determine which object type the instance belongs to, and then call the methods and properties in the corresponding class.

Listing 6.12 Benefits of dynamic binding

class User//user
{
protected function isauthorized ()//whether the user is authenticated
{
return (FALSE);
}

Public Function GetName ()//Get Name
{
Return ($this->name);
}

Public Function DeleteUser ($username)//delete user
{
if (! $this->isauthorized ())
{
Print ("You aren't authorized.
n ");
return (FALSE);
}

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

Class Authorizeduser extends user//Authenticated Users
{
protected function isauthorized ()//overwrite isauthorized ()
{
return (TRUE);
}
}

$user = new User;
$admin = new Authorizeduser;

Not authorized
$user->deleteuser ("Zeev");

Authorized
$admin->deleteuser ("Zeev");
? >

Why are private class members impersonating static bindings? To answer this question, you need to recall why you need a private member. When does it make sense to use them instead of protected members?

Private members are only used if you do not want subclasses to inherit changes or to specialize in the behavior of the parent class, which is less than you might think, and generally a good object hierarchy should allow the most functional quilts to specialize, improve, or change-this is one of the foundations of object-oriented programming. Certain cases require private methods or variables, such as when you are sure that you do not want to allow subclasses to change a particular part of the parent class.

http://www.bkjia.com/PHPjc/313905.html www.bkjia.com true http://www.bkjia.com/PHPjc/313905.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 accesses and variables in memory ...

  • 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.