Home > Backend Development > PHP Tutorial > Access methods and permissions of class members_PHP tutorial

Access methods and permissions of class members_PHP tutorial

WBOY
Release: 2016-07-15 13:25:06
Original
1000 people have browsed it

The access method of PHP5 allows restricting access to class members. This is a new feature in PHP5, but it already exists in many object-oriented languages. Only with the access method can we develop a reliable oriented Object applications, 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 1. 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 previous versions of PHP, all methods and properties were public, This makes objects look like well-structured arrays.

Private (private) members are only visible inside the class. You cannot change or read the value of a private property outside of the class method in which it resides. . 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 members in the class and instances of the class Both 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. 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 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 检查两个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>n");    }    //FALSE    if($w1->equals($w3))    {        print("w1 and w3 are the same<br>n");    }    //FALSE, == includes id in comparison    if($w1 == $w2) //不等,因为ID不同    {        print("w1 and w2 are the same<br>n");    } ?>
Copy after login

If you are new to object-oriented programming, you may want to know how to use private What is the purpose of members? 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 without 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 not be an implicit dependency.

Of course, most private properties can still be accessed External code sharing. The solution is to use a pair of public methods, one is get (get the value of the attribute), the other is set (set the value of the attribute). The constructor also accepts the initial value of the attribute. This allows 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 price to be a float number (floadval()).

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 to depend on a specific property. Writing a .protected method will not cause this concern. A subclass that uses a protected method needs to know the structure of its parent class.

Note that Widget now has a protected method called getName . If an instance of Widget tries to call the protected method, an error will occur: $w1->getName() generates an error. But the getName method in the subclass Thing can call this protected method. Of course, it is necessary to prove that the Widget::getName method is protected , this example seems too simple. In actual situations, using protected methods 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!) 结果仍然为真    if($w1->equals($w2))    {        print("w1 and w2 are the same<br>n");    }    //print Cog 输出 Cog    print($w2->getName()); ?>
Copy after login

A subclass may change by overriding the parent class method. Changing method access, however, still has 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 only visible within 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, you technically cannot override a private member.

The Final keyword is another way to restrict access to member methods. Subclasses cannot override methods marked as final in the parent class. The Final keyword cannot be used for attributes.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446721.htmlTechArticleThe access method of PHP5 allows restricting access to class members. This is a new feature in PHP5, but in It already exists in many object-oriented languages. With access methods, you can develop a reliable...
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