Php 简明教程
PHP – Access Modifiers
在 PHP 中,关键字 public, private 和 protected 被称为 access modifiers 。这些关键字控制类属性和方法的可访问性或可见性的范围。其中一个关键字在声明成员变量和定义成员函数时添加为前缀。
In PHP, the keywords public, private and protected are known as the access modifiers. These keywords control the extent of accessibility or visibility of the class properties and methods. One of these keywords is prefixed while declaring the member variables and defining member functions.
PHP 代码是否可以自由地访问类成员,或者被限制获取,或者有条件地访问,是由这些关键字决定的 -
Whether the PHP code has free access to a class member, or it is restricted from getting access, or it has a conditional access, is determined by these keywords −
-
Public − class members are accessible from anywhere, even from outside the scope of the class, but only with the object reference.
-
Private − class members can be accessed within the class itself. It prevents members from outside class access even with the reference of the class instance.
-
Protected − members can be accessed within the class and its child class only, nowhere else.
数据封装原则是面向对象编程方法的基础。它指的是将对象的成员数据或属性置于类外部环境的访问范围之外,只允许通过类中可用的方法或函数进行受控访问。
The principle of data encapsulation is the cornerstone of the object-oriented programming methodology. It refers to the mechanism of keeping the data members or properties of an object away from the reach of the environment outside the class, allowing controlled access only through the methods or functions available in the class.
要实现封装,类的成员数据被设为 private ,方法被设为 public 。
To implement encapsulation, data members of a class are made private and the methods are made public.
Public Members
在 PHP 中,类成员(成员变量和其他成员函数)默认情况下是公开的。
In PHP, the class members (both member variables as well as member functions) are public by default.
Example
在下面的程序中,对象的成员变量 title 和 price 在类的外部可以自由访问,因为它们默认情况下是公开的,除非另行指定。
In the following program, the member variables title and price of the object are freely accessible outside the class because they are public by default, if not otherwise specified.
<?php
class Book {
/* Member variables */
var $price;
var $title;
/*Constructor*/
function __construct(string $param1="PHP Basics", int $param2=380) {
$this->title = $param1;
$this->price = $param2;
}
function getPrice() {
echo "Title: $this->price \n";
}
function getTitle() {
echo "Price: $this->title \n";
}
}
$b1 = new Book();
echo "Title : $b1->title Price: $b1->price";
?>
它将生成以下 output −
It will produce the following output −
Title : PHP Basics Price: 380
Private Members
如上所述,封装原则要求不得直接访问成员变量。只有方法应具有对数据成员的访问权。因此,我们需要将成员变量设为私有,并将方法设为公有。
As mentioned above, the principle of encapsulation requires that the member variables should not be accessible directly. Only the methods should have the access to the data members. Hence, we need to make the member variables private and methods public.
<?php
class Book {
/* Member variables */
private $price;
private $title;
/*Constructor*/
function __construct(string $param1="PHP Basics", int $param2=380) {
$this->title = $param1;
$this->price = $param2;
}
public function getPrice() {
echo "Price: $this->price \n";
}
public function getTitle() {
echo "Title: $this->title \n;";
}
}
$b1 = new Book();
$b1->getTitle();
$b1->getPrice();
echo "Title : $b1->title Price: $b1->price";
?>
Output
现在,getTitle() 和 getPrice() 函数是公共的,能够访问私有成员变量 title 和 price。但是,如果尝试直接显示标题和价格,会遇到错误,因为它们不是公共的。
Now, the getTitle() and getPrice() functions are public, able to access the private member variables title and price. But, while trying to display the title and price directly, an error is encountered as they are not public.
Title: PHP Basics
Price: 380
Fatal error: Uncaught Error: Cannot access private property
Book::$title in hello.php:31
Protected Members
针对类成员指定受保护的访问权在类继承的情况下有效。我们知道可以从类的外围访问公有成员,并且不能从类的外围访问私有成员。
The effect of specifying protected access to a class member is effective in case of class inheritance. We know that public members are accessible from anywhere outside the class, and private members are denied access from anywhere outside the class.
protected 关键字允许访问同一类的对象和继承类中的对象,但禁止访问其他环境。
The protected keyword grants access to an object of the same class and an object of its inherited class, denying it to any other environment.
让我们在 Book 类示例中将标题成员设置为受保护的,将价格保留为私有的。
Let us set the title member in Book class example to protected, leaving price to private.
class Book {
/* Member variables */
private $price;
protected $title;
# rest of the code kept as it is
}
$b1 = new Book();
$b1->getTitle();
$b1->getPrice();
PHP 允许访问两个成员变量,因为该对象属于同一个类。
PHP allows the both the member variables to be accessed, as the object belongs to the same class.
让我们添加一个 mybook 类,它继承了 Book 类 −
Let us add a mybook class that inherits the Book class −
class mybook extends Book {
# no additional members defined
}
它的对象仍能访问成员变量,因为子类继承了父类的公有和受保护的成员。
whose object is still able to access the member variables, as the child class inherits public and protected members of the parent class.
但是,将 mybook 类作为一个独立的类(不扩展 Book 类)并定义一个 getmytitle() 函数,该函数尝试访问 Book 类的受保护标题成员变量。
However, make mybook class as an independent class (not extending Book class) and define a getmytitle() function that tries to access protected title member variable of Book class.
class mybook {
public function getmytitle($b) {
echo "Title: $b->title <br/>";
}
}
$b1 = new mybook();
$b = new Book();
$b1->getmytitle($b);
由于 getmytitle() 函数尝试打印 Book 对象的标题,因此一个显示 Cannot access protected property Book::$title 的错误消息被引发。
As the getmytitle() function tries to print title of Book object, an error message showing Cannot access protected property Book::$title is raised.
Example
尝试运行以下代码 −
Try to run the following code −
<?php
class Book {
private $price;
protected $title;
function __construct(string $param1="PHP Basics", int $param2=380) {
$this->title = $param1;
$this->price = $param2;
}
public function getPrice(){
echo "Price: $this->price <br/>";
}
public function getTitle(){
echo "Title: $this->title <br/>";
}
}
class mybook {
public function getmytitle($b) {
echo "Title: $b->title <br/>";
}
}
$b1 = new mybook();
$b = new Book();
$b1->getmytitle($b);
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught Error: Cannot access protected property
Book::$title in /home/cg/root/97848/main.php:18
因此,可以看到受保护的成员只能被同一类和继承类的对象访问。对于所有其他环境,受保护的成员不可访问。
Hence, it can be seen that the protected member is accessible by object of same class and inherited class only. For all other environment, protected members are not accessible.
可访问性规则可以由下表总结:
The accessibility rules can be summarized by the following table −