Section 8 - Access Method - Classes and Objects in PHP5 [8]_PHP Tutorial

WBOY
Release: 2016-07-13 17:20:47
Original
666 people have browsed it

Section 8 - Access Method

The access method of PHP5 allows restricting access to class members. This is a new feature in PHP5, but it has long been available in many object-oriented languages. Exists. Only with access methods can we develop a reliable object-oriented application and build a reusable object-oriented class library.

Like C++ and Java, PHP has three access methods: public, private and protected . The access method for 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 static members and put the access method 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 front of PHP In several versions, all methods and properties were public, which made the object look like a neatly structured array.

Private (private) members were only visible inside the class. You can't add a private property to a class. Change or read its value outside the class method. 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 of the class and instances of the class can access private members. Look at Example 6.8, the equals method compares two widgets. The == operator compares two objects of the same class, but in this example each Each object instance has a unique ID. The equals method only compares name and price. Note how the equals method accesses the private property of another Widget instance. Both Java and C allow this operation.

Listing 6.8 Private members

<?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 检查两个widget是否相同 

       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>
"); 

   } 



   //FALSE 

   if($w1->equals($w3)) 

   { 

       print("w1 and w3 are the same<br>
"); 

   } 



   //FALSE, == includes id in comparison 

   if($w1 == $w2) //不等,因为ID不同 

   { 

       print("w1 and w2 are the same<br>
"); 

   } 

?> 
Copy after login


If you are new to object-oriented programming, you may be wondering what the purpose of using private members is. You may 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 being accessed by code outside the class. They also help achieve loose coupling. If code outside the data structure cannot directly access the internal properties, then There will be no implicit correlation.

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 the value of the attribute), and the other is One is set (setting the value of a property). The constructor also accepts an initial value for the property. This allows communication between members to occur through a narrow, 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 (floadval()).

Protected members can be accessed by all methods in the same class and by inherited classes. Accessed by all methods in it. Public properties go against the spirit of encapsulation because they allow subclasses to be written that depend on a specific property. Protected methods do not bring this concern. A subclass using a protected method needs to be clear The structure of its parent class is sufficient.

Example 6.9 is improved from Example 6.8 and includes a Widget subclass Thing. Note that Widget now has a protected method called getName. If the Widget instance attempts Calling the protected method will cause an error: $w1->getName() generated an error. But 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 actual situations, using protected methods depends on understanding the internal structure of the object.

Listing 6.9 Protected members
<?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!) 结果仍然为真 

   if($w1->equals($w2)) 

   { 

       print("w1 and w2 are the same<br>
"); 

   } 



   //print Cog 输出 Cog 

   print($w2->getName()); 

?> 
Copy after login




A sub Classes may change the way methods are accessed by overriding superclass methods. However, there are still some restrictions. If you override a public class member, it must remain public in subclasses. If you override 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 a private member of the parent class will simply create a different member in the current class. Therefore, in technology You cannot override a private member.

The Final keyword is another way to restrict access to member methods. Subclasses cannot override methods marked 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 enough. For example, final cannot be used for Data, Method or even Class like in Java.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532555.htmlTechArticleSection 8 - Access Method The access method of PHP5 allows restricting access to class members. This is in PHP5 New features, but already existing in many object-oriented languages. With access methods,...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template