Section 8 -- access method -- ClassesandObjectsinPHP5 [8]

Source: Internet
Author: User
Tags float number
Section 8-access method PHP5 allows access to class members to be restricted. this is a new feature in PHP5, but it already exists in many object-oriented languages. with access, you can develop a reliable object-oriented application and build reusable object-oriented class libraries. like C ++ and Java, PHP has three accessors, SyntaxHighlighter. Section 8-access methods

PHP5 allows you to restrict access to class members. this is a new feature in PHP5, but it already exists in many object-oriented languages. with access, you can develop a reliable object-oriented application and build reusable object-oriented class libraries.

Like C ++ and Java, PHP has three access methods: public, private, and protected. the access method of a class member can be one of them. if you do not specify the access method, the default access method is public. you can also specify an access method for a static member before the static keyword (such as public static ).

Public members can be accessed without any restriction. any code outside the class can read and write the public attribute. you can call a public method from anywhere in the script. in the first few versions of PHP, all methods and attributes are public, which makes people think that the object is like an exquisite array.

Private (Private) members are only visible within the class. you cannot change or read the value of a private property outside of its class method. similarly, only a method in the same class can call a private method. the inherited subclass cannot access the private member of the parent class.

Note that any member or instance of the class can access the private member. see example 6.8. The equals method compares two widgets. = the operator compares two objects in the same class, but in this example, each object instance has a unique ID. the equals method only compares name and price. note how the equals method accesses the private attribute of another Widget instance. both Java and C allow such operations.

Listing 6.8 Private members
 Name = $ name; $ this-> price = floatval ($ price); $ this-> id = uniqid ();} // checks if two widgets are the same check whether the two widgets are the same public function equals ($ widget) {return ($ this-> name = $ widget-> name) AND ($ this-> price = $ widget-> price); }}$ w1 = new Widget (Cog, 5.00); $ w2 = new Widget (Cog, 5.00); $ w3 = new Widget (Gear, 7.00); // TRUE if ($ w1-> equals ($ w2) {print ("w1 and w2 are the same
");} // FALSE if ($ w1-> equals ($ w3) {print (" w1 and w3 are the same
");} // FALSE, = includes id in comparison if ($ w1 = $ w2) //, because the ID is different {print (" w1 and w2 are the same
") ;}?>


If you are not familiar with object-oriented programming, you may want to know what the purpose of using private members is. you can recall the ideas of encapsulation and coupling, which we have discussed at the beginning of this chapter. private members help encapsulate data. they can be hidden inside a class without being exposed to code outside the class. they also help to achieve loose coupling. if code outside the data structure cannot directly access internal properties, there will be no implicit associations.

Of course, most private attributes can still be shared by external code. the solution is to use a pair of public methods, one is get (get attribute value), and the other is set (set attribute value ). constructors also accept the initial values of attributes. this enables communication between members through a narrow and well-defined interface. this also provides the opportunity to change the value passed to the method. note: In example 6.8, how does the constructor force price to be a float number (floadval ()).

The Protected member can be accessed by all methods in the same class and all methods in the inherited class. public attributes violate the encapsulation spirit, because they allow subclass to be written based on a specific attribute. the protected method won't worry about this. A subclass using the protected method needs to be clear about its parent class structure.

Example 6.9 is improved by example 6.8 and contains a sub-class Thing of a Widget. note that the Widget now has a protected method called getName. if the Widget instance tries to call the protected method, an error occurs: $ w1-> getName. however, the getName method in the subclass Thing can call this protected method. of course, this example is too simple to prove that the Widget: getName method is protected. in practice, using the protected method depends on understanding the internal structure of the object.

Listing 6.9 Protected members
 Name = $ name; $ this-> price = floatval ($ price); $ this-> id = uniqid ();} // checks if two widgets are the same public function equals ($ widget) {return ($ this-> name ==$ widget-> name) AND ($ this-> price ==$ widget-> price);} protected function getName () {return ($ this-> name );}} class Thing extends Widget {private $ color; public function setColor ($ color) {$ this-> color = $ color;} public function getC Olor () {return ($ this-> color);} public function getName () {return (parent: getName () ;}$ w1 = new Widget (Cog, 5.00); $ w2 = new Thing (Cog, 5.00); $ w2-> setColor (Yellow); // TRUE (still !) The result is still true if ($ w1-> equals ($ w2) {print ("w1 and w2 are the same
");} // Print Cog output Cog print ($ w2-> getName ();?>




A subclass may change the method of access by overwriting the parent class method. However, there are still some restrictions. if you overwrite a public class member, its subclass must be public. if you overwrite a protected member, it can remain protected or public. the Private member is only visible in the current class. declaring a member with the same name as the private member of the parent class will create a different member in the current class. therefore, technically, you cannot override a private member.

The Final keyword is another method that restricts access to member methods. the subclass cannot overwrite the method identified as final in the parent class. The Final keyword cannot be used for attributes.

// Haohappy note: The PHP5 object-oriented model is still incomplete. for example, final can be used for Data, Method, and even Class as in Java.

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.