Section Nineth-Bindings-Classes and Objects in PHP5 [9]_php tutorial

Source: Internet
Author: User
Section Nineth-binding

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. 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 not possible to enhance access restrictions on class members. So it is impossible to change the way of access from public to protected.

Listing 6.10 Dynamic Binding
 
  Salutation ");            $this->identify ();        }        protected function Identify ()        {            print ("I am Father.
"); } }; Class Son extends Father { protected $salutation = "hey!"; The protected in the parent class $salutation overwritten with the protected function identify () //protected identify () in the parent class () overwrite { Print ("I am Son.")
"); } }; $obj = new Son (); $obj->getsalutation ();



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 value of salutation. The script binds this->salutation and the current class father. A similar principle applies to the Private method identify ().

Listing 6.11 Binding and private members
 
  Salutation ");            $this->identify ();        }        Private function Identify ()        {            print ("I am Father.
"); } } Class Son extends Father { Private $salutation = "hey!"; Private function Identify () { print ("I am Son.
"); } } $obj = new Son (); $obj->getsalutation (); Output Hello there! I am Father. ?>



The benefit of dynamic binding is that it allows inheriting classes to alter 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
 
  name);        }        Public Function DeleteUser ($username)//delete user        {            if (! $this->isauthorized ())            {                print ("You is not Authorized.
"); return (FALSE); } Delete the user print ("user deleted.
"); } } 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



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 specialize the behavior of the parent class. This situation is less than you think. Generally speaking, 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/532554.html www.bkjia.com true http://www.bkjia.com/PHPjc/532554.html techarticle Nineth-Binding In addition to restricting access, the way you access it 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, and the member accesses and changes ...

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