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. access to a class PHP5 class 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 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 access any code outside the. class without any restriction to read or write the public attribute. you can call a public method 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 the class method. Similarly, only a method in the same class can call a private method, and the inherited subclass cannot access the private member in 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. Pay attention to how the equals method accesses the private attribute of another Widget instance. both Java and C allow this operation.
Listing 6.8 Private members
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 n ");
}
// FALSE
If ($ w1-> equals ($ w3 ))
{
Print ("w1 and w3 are the same n ");
}
// FALSE, = Your des id in comparison
If ($ w1 = $ w2) // different, because the ID is different
{
Print ("w1 and w2 are the same 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 idea 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 implement 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 that in example 6.8, the constructor forces the price to be a float number (floadval ()).
Protected members 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 subclasses to be written based on a specific attribute. the protected method will not 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 the 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 (), but the getName method in the subclass Thing can call this protected method. of course, it is too simple to prove that the Widget: getName method is protected. in actual situations, the use of the protected method depends on the understanding of the internal structure of the object.
Listing 6.9 Protected members
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 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 way to restrict access to member methods. The subclass cannot override the method identified as final in the parent class, and the Final keyword cannot be used for attributes.