/* +-------------------------------------------------- --------------------------------+ | = This article is read by Haohappy> | = Classes and Objects 1 Chapter's notes | = Translation + personal experience | = To avoid unnecessary trouble, please do not reprint, thank you | = Criticisms and corrections are welcome, and I hope to make progress together with all PHP enthusiasts! +------- -------------------------------------------------- -----------------------+ */ Section 8 - Access methods The access methods of PHP5 allow restricting access to class members. This is new in PHP5 It is an added feature, but it already exists in many object-oriented languages. With access methods, you can develop a reliable object-oriented application and build a reusable object-oriented class library. Like C++ and Java, PHP has three access methods Mode: public, private and protected. The access mode of a class member can be one of them. If you do not specify the access mode, the default access mode is public. You can also specify an access mode for static members, and the access mode will be The method is placed before the static keyword (such as public static). Public members can be accessed without restrictions. Any code outside the class can read and write public properties. You can call a public method from anywhere in the script. In PHP In previous versions, all methods and properties were public, which made objects look like neatly structured arrays. Private members were only visible inside the class. You couldn't create a private member inside a class method. Change or read its value externally. Similarly, only methods in the same class can call a private method. Inherited subclasses cannot access private members in the parent class. It should be noted that any member in the class and the class Instances of can access private members. See Example 6.8, the equals method compares two widgets. The == operator compares two objects of the same class, but in this example each object instance has a unique ID. equals method Compares only name and price. Note how the equals method accesses the private property of another Widget instance. Both Java and C allow this. Listing 6.8 Private 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)); } } $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, == includes id in comparison if($w1 == $w2) //not equal because the IDs are different{ print("w1 and w2 are the same
n"); } ?> If you are not familiar with object-oriented programming, you may want to Know the purpose of using private members. You can recall the ideas of encapsulation and coupling, which we discussed at the beginning of this chapter. Private members help encapsulate data. They can be hidden inside a class from code outside the class. At the same time, they also help achieve loose coupling. If code outside the data structure cannot directly access the internal properties, then there will not be an implicit dependency. Of course, most private properties can still be shared by external code The solution is to use a pair of public methods, one is get (gets the value of the property) and the other is set (sets the value of the property). The constructor also accepts the initial value of the property. This allows communication between members through a narrow , through a well-defined interface. This also provides the opportunity to change the value passed to the method. Notice in Example 6.8 how the constructor forces price to be a float number (floadval()). Protected member Can be accessed by all methods in the same class and all methods in inherited classes. Public properties violate the spirit of encapsulation because they allow subclasses to rely on a specific property to write. Protected methods do not bring Concern in this regard. A subclass that uses a protected method needs to be very clear about the structure of its parent class. Example 6.9 is an improvement from Example 6.8 and includes a Widget subclass Thing. Note that Widget now has a class called getName The protected method. If the Widget instance tries to call the protected method, an error will occur: $w1->getName() generated an error. But the getName method in the subclass Thing can call this protected method. Of course, for the proof Widget::getName method is protected, this example seems too simple. In actual situations, 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 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
n"); } //print Cog output Cog print($w2->getName()); ?> A subclass may change the access method of the method by overriding the parent class method , nevertheless, there are still some restrictions. If you override a public class member, it must remain public in its subclasses. If you override a protected member, it can remain protected or become public. Private members still only exist in Visible in the current class. Declaring a member with the same name as a private member of the parent class will simply create a different member in the current class. Therefore, technically you cannot override a private member. The Final keyword restricts access Another method of member methods. Subclasses cannot override methods marked as final in the parent class. The Final keyword cannot be used for attributes. //haohappy Note: The object-oriented model of PHP5 is still not perfect, such as final is not like in Java It can be used for Data, Method and even Class.
http://www.bkjia.com/PHPjc/532158.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532158.htmlTechArticle/* +--------------------- -------------------------------------------------- --------+ | = This article is read by Haohappy> | = Notes from the Chapter Classes and Objects| = Translation + personal experience...