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

Access methods and permissions of PHP class members_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:37:221085browse

PHP5的访问方式允许限制对类成员的访问. 这是在PHP5中新增的功能,但在许多面向对象语言中都早已存在. 有了访问方式,才能开发一个可靠的面向对象应用程序,并且构建可重用的面向对象类库.

像C++和Java一样,PHP有三种访问方式:public,private和protected. 对于一个类成员的访问方式,可以是其中之一. 如果你没有指明访问方式,默认地访问方式为public. 你也可以为静态成员指明一种访问方式,将访问方式放在static关键字之前(如public static).

Public成员可以被毫无限制地访问.类外部的任何代码都可以读写public属性. 你可以从脚本的任何地方调用一个public方法. 在PHP的前几个版本中,所有方法和属性都是public, 这让人觉得对象就像是结构精巧的数组.

Private(私有)成员只在类的内部可见. 你不能在一个private属性所在的类方法之外改变或读取它的值. 同样地,只有在同一个类中的方法可以调用一个private方法. 继承的子类也不能访问父类中的private 成员.

要注意,类中的任何成员和类的实例都可以访问private成员. 看例子6.8,equals方法将两个widget进行比较.==运算符比较同一个类的两个对象,但这个例子中每个对象实例都有唯一的ID.equals 方法只比较name和price. 注意equals方法如何访问另一个Widget实例的private属性. Java和C都允许这样的操作.

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

   //FALSE
   if($w1->equals($w3))
   {
       print("w1 and w3 are the same ");
   }

   //FALSE, == includes id in comparison
   if($w1 == $w2) //不等,因为ID不同
   {
       print("w1 and w2 are the same ");
   }
?>

如果你对面向对象编程不熟悉,你可能想知道用private成员的目的是什么. 你可以回忆一下封装和耦合的想法,这在本章开头我们有讨论过. Private成员有助于封装数据. 他们可以隐藏在一个类内部而不被类外部的代码接触到. 同时他们还有助于实现松散的耦合. 如果数据结构外的代码不能直接访问内部属性,那么就不会产生一个隐性的关联性.

当然,大部分private属性仍然可以被外部代码共享. 解决方法是用一对public方法,一个是get(获取属性的值),另一个是set(设置属性的值). 构造函数也接受属性的初始值. 这使得成员间的交流通过一个狭窄的,经过良好限定的接口来进行. 这也提供改变传递给方法的值的机会. 注意在例子6.8中,构造函数如何强制使price成为一个float数(floadval()).

Protected(受保护的) 成员能被同个类中的所有方法和继承出的类的中所有方法访问到. Public属性有违封装的精神,因为它们允许子类依赖于一个特定的属性来书写.protected方法则不会带来这方面的担忧.一个使用 protected方法的子类需要很清楚它的父类的结构才行.

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() generated an error. But the getName method in the subclass Thing can be called 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.


<?php
class Widget
{
private $name;
private $price;
private $id;

public function __construct($name, $price)
{
name = $name;
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 ");
}

//print Cog output 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 for attributes.

http://www.bkjia.com/PHPjc/486583.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486583.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...
Statement:
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