PHP5 access 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 a reusable object-oriented class library.
Like C + + and Java, PHP has three ways to access it: public,private and protected. This can be one of the ways that a class member is accessed. If you do not specify the access method, the default access method is public. You can also specify a way to access static members, placing access before the static keyword, such as public static.
Public members can be accessed without restrictions. Any code outside the class can read and write to the public property. You can call a public method from anywhere in the script. In the first few versions of PHP, all methods and properties are public, which makes the object seem like an array of ingenious structures.
Private (private) members are visible only inside the class. You cannot change or read its value outside of the class method where the private property resides. Similarly, only methods in the same class can invoke a private method. Inherited subclasses also cannot access private members in the parent class.
Note that any member of the class and an instance of the class can access the private member. Look at the example 6.8,equals method to compare two widgets. The = = operator Compares two objects of the same class, but each object instance in this example has a unique Id.equals method that compares name and price only. Note how the Equals method accesses the private property of another widget instance. Both Java and C allow such operations.
n"); }//false if ($w 1->equals ($w 3)) {print ("W1 and W3 are the same n"); }//false, = = includes ID in comparison if ($w 1 = = $w 2)//varies, because the IDs are different {print ("W1 and W2 are the same n"); }? > |
If you are not familiar with programming to objects, 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 achieve loose coupling. If code outside the data structure does not directly access internal properties, then there is no implicit association.
Of course, most private properties can still be shared by external code. The workaround is to use a pair of public methods, one is get (gets the value of the property) and the other is set (the value of the property is set). The constructor also accepts the initial value of the property. This allows for communication between members through a narrow, well-defined interface. This also provides the opportunity to change the value passed to the method. Note In Example 6.8, how the constructor forces the price to be a float number (Floadval ()).
A Protected (protected) member can be accessed by all methods in the same class and all methods in the inherited class. The public attribute is incompatible with the encapsulation spirit, because they allow subclasses to rely on a specific attribute to write. The protected method does not cause this concern. A subclass that uses the protected method needs to be clear about the structure of its parent class.
Note that the widget now has a protected method called GetName. An error occurs if an instance of the widget tries to invoke the protected method: $w 1->getname (). However, the GetName method in 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, the use of the protected method relies on an understanding of 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 widgets is 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 ()); }} $w 1 = new Widget (' Cog ', 5.00); $w 2 = new Thing (' Cog ', 5.00); $w 2->SetColor (' Yellow '); True (still!) The result is still true if ($w 1->equals ($w 2)) {print ("W1 and W2 are the same n "); }//print COG Output cog Print ($w 2->getname ());? |
A subclass may change the way the method is accessed by overwrite the parent class method, although there are still some limitations. If you overwrite a public class member, you must keep public in his subclass. If you overwrite a protected member, it can remain protected or become public. Private members are still visible only in the current class. Declaring a member with the same name as the private member of the parent class will simply create a different member in the current class. Therefore, technically you cannot overwrite a private member.
The final keyword is another way to restrict access to member methods. Subclasses cannot overwrite a method that is identified as final in a parent class. The final keyword cannot be used for attributes.
http://www.bkjia.com/PHPjc/446721.html www.bkjia.com true http://www.bkjia.com/PHPjc/446721.html techarticle PHP5 Access 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 to the way to develop a reliable ...