The access method and permissions of PHP class members. The access method of PHP5 allows access to class members. this is a new feature in PHP5, but it already exists in many object-oriented languages. with the access method, you can develop a reliable
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.
<? 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 (gears, 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, = Your des id in comparison
If ($ w1 = $ w2) // different, 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.
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.
<? 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 (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.
Features. this is a new feature in PHP5, but it already exists in many object-oriented languages. with access methods, you can develop a reliable one...