Home  >  Article  >  Backend Development  >  PHP object-oriented learning tutorial 5

PHP object-oriented learning tutorial 5

黄舟
黄舟Original
2016-12-29 10:59:591038browse

9. Encapsulation
Encapsulation is one of the three major characteristics of object-oriented programming. Encapsulation is to combine the properties and services of an object into an independent
identical unit, and to hide the properties of the object as much as possible Internal details include two meanings: 1. Combining all attributes and services of the object to form an indivisible independent unit (i.e. object). 2. Information hiding, that is, hiding the internal details of the object as much as possible, forming a boundary (or forming a barrier) to the appearance, and only retaining a limited external interface to connect it with the outside.
The reflection of the principle of encapsulation in software is: it requires that parts other than the object cannot access the internal data (properties) of the object at will,
thus effectively avoiding the "cross-infection" of external errors and making it Software errors can be localized, greatly reducing the difficulty of error checking and troubleshooting.
Let’s use an example to illustrate. If a person’s object has attributes such as age and salary, such personal privacy attributes are not
allowed to be obtained by others at will. If you don’t use encapsulation, Then others can get it if they want to know, but if you encapsulate it, others will have no way to obtain the encapsulated attributes. Unless you tell it yourself, others will have no way to get it.
For another example, personal computers have a password, and you don’t want others to log in at will and copy and paste it into your computer.
Also, for objects like people, the attributes of height and age can only be increased by oneself, and cannot be assigned values ​​arbitrarily by others, etc.
Use the private keyword to encapsulate properties and methods:
Original members:

var $name; //声明人的姓名
var $sex; //声明人的性别
var $age; //声明人的年龄
function run(){… … .}

Change to encapsulated form:

private $name; //把人的姓名使用private 关键字进行封装
private $sex; //把人的性别使用private 关键字进行封装
private $age; //把人的年龄使用private 关键字进行封装
private function run(){… … } //把人的走路方法使用private 关键字进行封装

Note : As long as there are other keywords in front of the member attributes, the original keyword "var" must be removed.

You can encapsulate human members (member attributes and member methods) through private. The members on the package cannot be directly accessed by
outside the class, only the object itself can access it; the following code will generate an error:

class Person
{
//下面是人的成员属性
private $name; //人的名字,被private封装上了
private $sex; //人的性别, 被private封装上了
private $age; //人的年龄, 被private封装上了
//这个人可以说话的方法
function say()
{
echo "我的名字叫:".$this->name." 性别:".$this->sex." 我的年龄是:
".$this->age."<br>";
}/
/这个人可以走路的方法, 被private封装上了
private function run()
{
echo "这个人在走路";
}
}
//实例化一个人的实例对象
$p1=new Person();
//试图去给私有的属性赋值, 结果会发生错误
$p1->name="张三";
$p1->sex="男";
$p1->age=20;
//试图去打印私有的属性, 结果会发生错误
echo $p1->name.”<br>”;
echo $p1->sex.”<br>”;
echo $p1->age.”<br>”
//试图去打印私有的成员方法, 结果会发生错误
$p1->run();

The output result is:

Fatal error: Cannot access private property Person::$name
Fatal error: Cannot access private property Person::$sex
Fatal error: Cannot access private property Person::$age
Fatal error: Cannot access private property Person::$name
Fatal error: Call to private method Person::run() from context &#39;&#39;

As you can see from the above example, private members cannot be accessed externally, because private members can only be accessed within this object. For example, the $p1 object wants to access its The private attributes are said, and the private attributes are accessed in the say() method.

This is OK. (No access control is added. The default is public and can be accessed anywhere)

//这个人可以说话的方法, 说出自己的私有属性,在这里也可以访问私有方法
function say()
{
echo "我的名字叫:".$this->name." 性别:".$this->sex." 我的年龄是:
".$this->age."<br>";
//在这里也可以访问私有方法
//$this->run();
}
因为成员方法say()是公有的, 所以我们在类的外部调用say()方法是可以的,改变上面的代码;
class Person
{
//下面是人的成员属性
private $name; //人的名字,被private封装上了
private $sex; //人的性别, 被private封装上了
private $age; //人的年龄, 被private封装上了
//定义一个构造方法参数为私有的属性姓名$name、性别$sex和年龄$age进行赋值
function __construct($name, $sex, $age)
{
//通过构造方法传进来的$name给私有成员属性$this->name赋初使值
$this->name=$name;
//通过构造方法传进来的$sex给私有成员属性$this->sex赋初使值
$this->sex=$sex;
//通过构造方法传进来的$age给私有成员属性$this->age赋初使值
$this->age=$age;
}/
/这个人可以说话的方法, 说出自己的私有属性,在这里也可以访问私有方法
function say()
{
echo "我的名字叫:".$this->name." 性别:".$this->sex." 我的年龄是:
".$this->age."<br>";
}
}
//通过构造方法创建3个对象$p1、p2、$p3,分别传入三个不同的实参为姓名、性别和年龄
$p1=new Person(“张三”,”男”, 20);
$p2=new Person(“李四”,”女”, 30);
$p3=new Person(“王五”,”男”, 40);
//下面访问$p1对象中的说话方法
$p1->say();
//下面访问$p2对象中的说话方法
$p2->say();
//下面访问$p3对象中的说话方法
$p3->say();

The output result is:

我的名字叫:张三性别:男我的年龄是:20
我的名字叫:李四性别:女我的年龄是:30
我的名字叫:王五性别:男我的年龄是:40

Because the constructor is public by default method (the constructor method should not be set as private), so it can be accessed outside the class.

In this way, the constructor method can be used to create objects. In addition, the constructor method is also a function inside the class, so the constructor method can be used to privately

Some attributes are assigned initial values. The Say() method is public by default, so it can be accessed from the outside and tell its own private properties.
We can see from the above example that private members can only be used inside the class and cannot be directly accessed by outside the class.

But they have permission to access inside the class, so sometimes We need to assign and read private properties outside the class, which means

provides some accessible interfaces outside the class. In the above example, the constructor method is a form of assignment, but the constructor method only
is assigned when creating an object. If we already have an existing object and want to assign a value to this existing object, at this time
, if you also use the constructor method to pass value, then create A new object is created, which is not the existing object
. Therefore, we need to make some interfaces for private attributes that can be accessed externally. The purpose is to
change and access the value of the attribute while the object exists. However, it should be noted that only the attributes that need to be changed externally can be changed. In this way, the properties that are
not intended to be accessed by the outside world do not have such interfaces, so that the purpose of encapsulation can be achieved. All functions are completed by the objects themselves, providing
as few operations as possible to the outside world.
If you provide an interface outside the class, you can provide setting methods and get methods for private properties outside the class to operate the private properties.
For example:

prvate $age; //私有的属性年龄
function setAge($age) //为外部提供一个公有设置年龄的方法
{
if($age<0 || $age>130) //在给属性赋值的时候,为了避免非法值设置给属性
return;
$this->age=$age;
}f
unction getAge() //为外部提供一个公有获取年龄的方法
{
return($this->age);
}

上面的方法是为一个成员属性设置和获取值, 当然你也可以为每个属性用同样的方法对其进行
赋值和取值的操作,完成在类外部的存取工作。
10.__set() __get() __isset() __unset()四个方法的应用
一般来说,总是把类的属性定义为private,这更符合现实的逻辑。但是,对属性的读取和赋值
操作是非常频繁的,因此在PHP5 中,预定义了两个函数“__get()”和“__set()”来获取和赋值其
属性,以及检查属性的“__isset()”和删除属性的方法“__unset()”。
上一节中,我们为每个属性做了设置和获取的方法,在PHP5 中给我们提供了专门为属性设置
值和获取值的方法,“__set()”和“__get()”这两个方法,这两个方法不是默认存在的,而是我们手
工添加到类里面去的,像构造方法(__construct())一样, 类里面添加了才会存在,可以按下面的方式
来添加这两个方法,当然也可以按个人的风格来添加:
//__get()方法用来获取私有属性

private function __get($property_name)
{
if(isset($this->$property_name))
{
return($this->$property_name);
}else
{
return(NULL);
}
}/
/__set()方法用来设置私有属性
private function __set($property_name, $value)
{
$this->$property_name = $value;
}

__get()方法:

这个方法用来获取私有成员属性值的,有一个参数,参数传入你要获取的成员属性
的名称,返回获取的属性值,这个方法不用我们手工的去调用,因为我们也可以把这个方法做成私
有的方法,是在直接获取私有属性的时候对象自动调用的。因为私有属性已经被封装上了,是不能
直接获取值的(比如:“echo $p1->name”这样直接获取是错误的),但是如果你在类里面加上了这
个方法,在使用“echo $p1->name”这样的语句直接获取值的时候就会自动调用__get($property_name)
方法,将属性name 传给参数$property_name,通过这个方法的内部执行,返回我们传入的私有属性
的值。如果成员属性不封装成私有的,对象本身就不会去自动调用这个方法。
__set()方法:这个方法用来为私有成员属性设置值的,有两个参数,第一个参数为你要为设置
值的属性名,第二个参数是要给属性设置的值,没有返回值。这个方法同样不用我们手工去调用,
它也可以做成私有的,是在直接设置私有属性值的时候自动调用的,同样属性私有的已经被封装上
了, 如果没有__set()这个方法,是不允许的, 比如:$this->name=‘zhangsan’, 这样会出错,但
是如果你在类里面加上了__set($property_name, $value)这个方法,在直接给私有属性赋值的时候,
就会自动调用它,把属性比如name 传给$property_name, 把要赋的值“zhangsan”传给$value,通过
这个方法的执行,达到赋值的目的。如果成员属性不封装成私有的,对象本身就不会去自动调用这
个方法。为了不传入非法的值, 还可以在这个方法给做一下判断。代码如下:

<?php
class Person
{
//下面是人的成员属性, 都是封装的私有成员
private $name; //人的名字
private $sex; //人的性别
private $age; //人的年龄
//__get()方法用来获取私有属性
private function __get($property_name)
{
echo "在直接获取私有属性值的时候,自动调用了这个__get()方法<br>";
if(isset($this->$property_name))
{
return($this->$property_name);
}e
lse
{
return(NULL);
}
}/
/__set()方法用来设置私有属性
private function __set($property_name, $value)
{
echo "在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值<br>";
$this->$property_name = $value;
}
}
$p1=new Person();
//直接为私有属性赋值的操作, 会自动调用__set()方法进行赋值
$p1->name="张三";
$p1->sex="男";
$p1->age=20;
//直接获取私有属性的值, 会自动调用__get()方法,返回成员属性的值
echo "姓名:".$p1->name."<br>";
echo "性别:".$p1->sex."<br>";
echo "年龄:".$p1->age."<br>";
?>

程序执行结果:

在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值
在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值
在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值
在直接获取私有属性值的时候,自动调用了这个__get()方法
姓名:张三
在直接获取私有属性值的时候,自动调用了这个__get()方法
性别:男
在直接获取私有属性值的时候,自动调用了这个__get()方法
年龄:20

以上代码如果不加上__get()和__set()方法,程序就会出错,因为不能在类的外部操作私有成员,
而上面的代码是通过自动调用__get()和__set()方法来帮助我们直接存取封装的私有成员的。
__isset() 方法:在看这个方法之前我们看一下“isset()”函数的应用,isset()是测定变量是否设
定用的函数,传入一个变量作为参数,如果传入的变量存在则传回true,否则传回false。那么如果
在一个对象外面使用“isset()”这个函数去测定对象里面的成员是否被设定可不可以用它呢?分两种
情况,如果对象里面成员是公有的,我们就可以使用这个函数来测定成员属性,如果是私有的成员
属性,这个函数就不起作用了,原因就是因为私有的被封装了,在外部不可见。那么我们就不可以
在对象的外部使用“isset()”函数来测定私有成员属性是否被设定了呢?可以,你只要在类里面加上
一个“__isset()”方法就可以了,当在类外部使用”isset()”函数来测定对象里面的私有成员是否被设
定时, 就会自动调用类里面的“__isset()”方法了帮我们完成这样的操作,“__isset()”方法也可以
做成私有的。你可以在类里面加上下面这样的代码就可以了:

private function __isset($nm)
{
echo "当在类外部使用isset()函数测定私有成员$nm时,自动调用<br>";
return isset($this->$nm);
}

__unset()方法:

看这个方法之前呢,我们也先来看一下“unset()”这个函数,“unset()”这个函
数的作用是删除指定的变量且传回true,参数为要删除的变量。那么如果在一个对象外部去删除对
象内部的成员属性用“unset()”函数可不可以呢,也是分两种情况,如果一个对象里面的成员属性
是公有的,就可以使用这个函数在对象外面删除对象的公有属性,如果对象的成员属性是私有的,
我使用这个函数就没有权限去删除,但同样如果你在一个对象里面加上“__unset()”这个方法,就
可以在对象的外部去删除对象的私有成员属性了。在对象里面加上了“__unset()”这个方法之后,
在对象外部使用“unset()”函数删除对象内部的私有成员属性时,自动调用“__unset()”函数来帮
我们删除对象内部的私有成员属性,这个方法也可以在类的内部定义成私有的。在对象里面加上下
面的代码就可以了:

private function __unset($nm)
{
echo "当在类外部使用unset()函数来删除私有成员时自动调用的<br>";
unset($this->$nm);
}
我们来看一个完整的实例:
<?php
class Person
{
//下面是人的成员属性
private $name; //人的名字
private $sex; //人的性别
private $age; //人的年龄
//__get()方法用来获取私有属性
private function __get($property_name)
{
if(isset($this->$property_name))
{
return($this->$property_name);
}else {
return(NULL);
}
}/
/__set()方法用来设置私有属性
private function __set($property_name, $value)
{
$this->$property_name = $value;
}/
/__isset()方法
private function __isset($nm)
{
echo "isset()函数测定私有成员时,自动调用<br>";
return isset($this->$nm);
}/
/__unset()方法
private function __unset($nm)
{
echo "当在类外部使用unset()函数来删除私有成员时自动调用的<br>";
unset($this->$nm);
}
}
$p1=new Person();
$p1->name="this is a person name";
//在使用isset()函数测定私有成员时,自动调用__isset()方法帮我们完成,返回结果为true
echo var_dump(isset($p1->name))."<br>";
echo $p1->name."<br>";
//在使用unset()函数删除私有成员时,自动调用__unset()方法帮我们完成,删除name私有属性
unset($p1->name);
//已经被删除了, 所这行不会有输出
echo $p1->name;
?>

输出结果为:

isset()函数测定私有成员时,自动调用
bool(true)
this is a person name
当在类外部使用unset()函数来删除私有成员时自动调用的
__set()、__get()、__isset()、__unset() 这四个方法都是我们添加到对象里面的,在需要时自动调
用的,来完成在对象外部对对象内部私有属性的操作

 以上就是php面向对象学习教程5的内容,更多相关内容请关注PHP中文网(www.php.cn)!


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