Section 8-access methods

Source: Internet
Author: User
Tags float number

/*
+ ------------------------------------------------------------------------------- +
| = 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!
| = PHP5 site: http://blog.csdn.net/haohappy2004
+ ------------------------------------------------------------------------------- +
*/
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 Copy codeThe Code is as follows: <? Php
Class Widget
{
Private $ name;
Private $ price;
Private $ id;
Public function _ construct ($ name, $ price)
{
$ This-> 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 <br> \ n ");
}
// FALSE
If ($ w1-> equals ($ w3 ))
{
Print ("w1 and w3 are the same <br> \ n ");
}
// FALSE, = Your des id in comparison
If ($ w1 = $ w2) // different, because the ID is different
{
Print ("w1 and w2 are the same <br> \ n ");
}
?>

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 membersCopy codeThe Code is as follows: <? Php
Class Widget
{
Private $ name;
Private $ price;
Private $ id;
Public function _ construct ($ name, $ price)
{
$ This-> 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 getColor ()
{
Return ($ this-> color );
}
Public function getName ()
{
Return (parent: getName ());
}
}
$ W1 = new Widget ('cog', 5.00 );
$ W2 = new Thing ('cogb', 5.00 );
$ W2-> setColor ('yellow ');
// TRUE (still !) The result is still true.
If ($ w1-> equals ($ w2 ))
{
Print ("w1 and w2 are the same <br> \ n ");
}
// 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.

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.