Php 简明教程
PHP - Encapsulation
PHP 实现了 encapsulation ,这是 OOP 的一个重要原则,带有访问控制关键字 public, private 和 protected 。
PHP implements encapsulation, one of the important principles of OOP with access control keywords: public, private and protected.
封装是指将对象的数据成员或属性远离类外部环境获取的方式,仅允许通过类中可用的方法或函数进行受控访问。
Encapsulation 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.
下图展示了面向对象编程方法中封装的原则。
The following diagram illustrates the principle of encapsulation in object-oriented programming methodology.
PHP 的关键字列表包含以下关键字,这些关键字决定了 PHP 中对象实例的属性和方法的可访问性 −
PHP’s keywords list contains the following keywords that determine the accessibility of properties and methods of an object, which is an instance of a class in PHP −
-
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.
这三个关键字“ public, private 和 protected ”通常称为访问修饰符。它们也被称为可见性模式,因为它们决定了某类成员可以使用的范围。
These three keywords "public, private and protected" are often called access modifiers. They are also referred as visibility modes, as they decide upto what extent a certain class member is available.
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 Person {
/* Member variables */
var $name;
var $age;
/*Constructor*/
function __construct(string $param1="Ravi", int $param2=28) {
$this->name = $param1;
$this->age = $param2;
}
function getName() {
echo "Name: $this->name" . PHP_EOL;;
}
function getAge() {
echo "Age: $this->age" . PHP_EOL;;
}
}
$b1 = new Person();
$b1->getName();
$b1->getAge();
echo "Name : $b1->name Age: $b1->age" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Name: Ravi
Age: 28
Name : Ravi Age: 28
Note 由于默认情况下类成员的所有属性都为公有,因此如果需要,可以明确地宣告它们为公有。结果,可以从类的外围调用实例方法 getName() 和 getAge()。
Note that the properties all the class members are public by default, you can explicitly declare them as public if desired. As a result, the instance methods getName() and getAge() can be called from outside the class.
由于属性 name 和 age 也是公有的,因此也可以从类外访问它们,这与封装原则相违背。
Since properties name and age are also public, hence they can also be accessed outside the class, something which is not desired as per the principle of encapsulation.
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.
Example
让我们将 name 和 age 属性的宣告改为私有,并运行以下 PHP 脚本:
Let us change the declaration of name and age properties to private and run the following PHP script −
<?php
class Person {
/* Member variables */
private $name;
private $age;
/*Constructor*/
function __construct(string $param1="Ravi", int $param2=28) {
$this->name = $param1;
$this->age = $param2;
}
public function getName() {
echo "Name: $this->name" . PHP_EOL;;
}
public function getAge(){
echo "Age: $this->age" . PHP_EOL;;
}
}
$b1 = new Person();
$b1->getName();
$b1->getAge();
echo "Name : $b1->name Age: $b1->age" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Name: Ravi
Age: 28
PHP Fatal error: Uncaught Error: Cannot access private property Person::$name in person.php:27
该错误消息表明我们不能从公有范围访问私有属性。
The error message tells the reason that a private property cannot be accessed from a public scope.
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.
Example
让我们继承 person 类并定义一个学生类。我们将 name 属性的私有级别更改为受保护级别。此学生类具有一个新的公有方法 getDetails(),它将打印 name 和 age 属性的值。
Let us inherit the person class and define a student class. We shall change the name property from private to protected. The student class has a new public method getDetails() that prints the values of name and age properties.
Person class
Person class
<?php
class Person {
/* Member variables */
protected $name;
private $age;
/*Constructor*/
function __construct(string $param1="Ravi", int $param2=28) {
$this->name = $param1;
$this->age = $param2;
}
public function getName(){
echo "Name: $this->name" . PHP_EOL;;
}
public function getAge() {
echo "Age: $this->age" . PHP_EOL;;
}
}
Student class
Student class
class student extends Person {
public function getDetails() {
echo "My Name: $this->name" . PHP_EOL;
echo "My age: $this->age" . PHP_EOL;
}
}
$s1 = new student();
$s1->getDetails();
?>
它将生成以下 output −
It will produce the following output −
My Name: Ravi
PHP Warning: Undefined property: student::$age in person.php on line 28
My age:
下表说明了 PHP 中类成员的可访问性规则:
The following table illustrates the rules of accessibility of class members in PHP −