Section 8--Access Method_PHP Tutorial

WBOY
Release: 2016-07-21 16:01:02
Original
804 people have browsed it

/*
+-------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy<>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/
Section 8--Access Method
PHP5's access method allows restricting access to class members. This is a new feature in PHP5, but it has long existed 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++ is the same as 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 use static Members specify an access method, and the access 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 read and write public properties from Call a public method anywhere in the script. In previous versions of PHP, all methods and properties were public, which made objects feel like well-structured arrays.
Private members were only in classes Visible internally. You cannot change or read the value of a private property outside the class method in which it resides. Likewise, only methods in the same class can call a private method. Inherited subclasses cannot access the parent class. private members in.
It should be noted that any member in the class and instances of the class can access private members. See Example 6.8, the equals method compares two widgets. The == operator compares two widgets of the same class. Object, 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 property of another Widget instance. Both Java and C allow this operation.
Listing 6.8 Private members

Copy code The code is as follows:
   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
n");  
   }  
   //FALSE  
   if($w1->equals($w3))  
   {  
       print("w1 and w3 are the same
n");  
   }  
   //FALSE, == includes id in comparison  
   if($w1 == $w2) //不等,因为ID不同  
   {  
       print("w1 and w2 are the same
n");  
   }  
?>  
If you are new to object-oriented programming, you may be wondering what the purpose of 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 Hidden within a class and not accessible 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 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 (get the value of the property), the other is set (set the value of the property). The constructor also accepts The initial value of the property. This allows communication between members to occur through a narrow, well-qualified interface. This also provides the opportunity to change the value passed to the method. Note in Example 6.8 how the constructor forces price to be a float number (floadval()).
Protected (protected) members 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 rely on a specific attribute to be written. Protected methods do not cause this concern. A subclass that uses a protected method needs to be very clear about the structure of its parent class.
Example 6.9 is derived from Example 6.8 Improved to include a Widget subclass, Thing. Note that Widget now has a protected method called getName. If an instance of Widget attempts 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, this example is too simple to prove that the Widget::getName method is protected. In actual situations, using the protected method depends on understanding the internal structure of the object.
Listing 6.9 Protected members
Copy code The code is as follows:
   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
n");  
   }  
   //print Cog 输出 Cog  
   print($w2->getName());  
?> 

A subclass may change the way the method is accessed by overriding the parent class method. However, there are still some restrictions. If you override a public class member, it must remain public in the subclass. If you override You write a protected member, which 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, technically 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 Properties.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316944.htmlTechArticle/* +--------------------- -------------------------------------------------- --------+ |=This article is Haohappy's notes from the chapter ClassesandObjects when reading CorePHP Programming |=Translation is mainly + personal...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!