Home php教程 php手册 php学习笔记 php中面向对象三大特性之一[封装性]的应用

php学习笔记 php中面向对象三大特性之一[封装性]的应用

Jun 21, 2016 am 08:54 AM
age gt private this

复制代码 代码如下:


/*
* 封装性:面向对象三大特性之一
*
* 1.就是把对象的成员(属性,方法)结合成一个独立的相同单位,并尽可能隐藏对象的内部细节
* 访问权限修饰符 public protected private
* private:私有的,用这个关键字修饰的成员,只能在对象内部访问(只有用$this访问)
*
* 属性可以封装:
* 只要一个变量,需要在多个方法使用,就将这个变量声明为成员属性,可以直接在这个对象中的所有方法中使用
*
* 成员属性,相当于这个对象中的全局变量
*
* 成员属性都会在方法中使用,成员属性值的变化其实就是在改变方法的执行行为,也就是改变了对象的功能
*
* 成员属性的值如果不正常,方法执行的功能叶就不正常
*
* 作用:不需要在对象外部改变或读取它的值
* 1.封装
* 再提供一个公有的方法(经过方法对对象成员属性进行赋值和取值就可以控制)
*
*
* 方法也可以封装
*
* 作用:
* 1.使用private修饰使其只能在内部使用
*
* 2.一个类中有100个方法,封装了95个(为另外5个服务的方法),只有5个方法可以使用
*
* 100个成员属性,都让取值,不可以改值;或者只可改值,不可获取值//此种情况下使用以下方法比较方便
* 和封装有关的魔术方法:
*
* __set();//是直接设置[私有的]成员属性值时,自动调用的方法
* __get();//是直接获取[私有的]成员属性值时,自动调用的方法
* __isset();//是直接使用isset()查看对象中私有属性是否存在时,自动调用这个方法
* __unset();//是直接使用unset()删除对象中私有属性时,自动调用的方法
*
*
*
*
*
*
*
*/
class Person{
//x封装成员属性,不需要在对象外部改变
private $name;
private $age;
private $sex;
private __unset($proName){
unset($this->$proName);
}
//是直接查看对象中私有属性是否存在时,自动调用这个方法
//__isset($proName)的使用,$proName代表属性名
private function __isset($proName){
return isset($this->$proName);//isset()返回是否存在
}
function __construct($name,$age,$sex){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
//当获取私有的成员属性时,自动调用此方法
private function __get($proName)
{
//控制所获取的值
if($proName=="age"){
if($this-age>40)
return $this->age-10;
}
return $this->$proName;
}
//当设置私有的成员属性时,自动调用此方法
private function __set($proName,$proValue){
//$proName表示成员属性名,$proValue表示成员属性值
//控制设置范围
if($proName=="age"){
if($proValue > 100 $proValuereturn;
}
$this->$proName=$proValue;
}
//提供公有方法来设置成员属性的值
function setAge($age){
//控制年龄范围,增加安全性
if($age > 100 $age return;
$this->age=$age;
}
//提供公有方法来获取成员属性的值
function getAge(){
//控制获取年龄的范围
if($this->age return $this->age;
else if($this->agereturn $this->age-5;
else if($this->agereturn $this->age;
else
return $this->age-15;
提供公有方法来 }
function say(){
echo "我的名字:{$this->name},我的年龄:{$this->age},我的姓别:{$this->sex}
";
//访问封装过的 run()方法
$this-run();
}
private function run(){
echo '111111111111
'
}
function eat(){
}
//析构方法
function __destruct(){
}
}
$p1=new Person("zhangsan",25,"男");
$p2=new Person;
$p3=new Person;
//$p1->age=-50;//因为年龄在外部随意访问,所以成员属性药封装,才有安全性。
$p1->setAge(30);//通过方法来设置成员属性德值
$p1->getAge();//通过方法来获取成员属性德值
//通过添加家魔术方法__set($proName,$proValue) __get($proName),就可以直接调用成员属性了
$p1->say();//可以调用
$p1->run();//私有的方法不能直接调用
//删除$p1里面的name
unset($p1->name);
//判断name是否存在
if(isset($p1->name)){
echo "存在
";
}else{
echo "没有这个成员
";
}
?>


作者:代号极光
出处:http://zizhuyuan.cnblogs.com



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What does private mean in java What does private mean in java Nov 24, 2022 pm 06:27 PM

In Java, private means "private" and is an access control modifier used to modify classes, properties and methods. Class members modified with private can only be accessed and modified by the methods of the class itself, and cannot be accessed and referenced by any other class (including subclasses of the class); therefore, the private modifier has the highest level of protection.

age animation animation official entrance age animation animation official entrance Feb 23, 2024 pm 09:49 PM

age animation is a software that can provide users with various animations, so what is the official entrance of the software? Users only need to enter the URL below to watch various animations for free. They may encounter network problems when logging in. This sharing of the official entrance of age anime animation can tell you what the specific entrance is, come and take a look! Where is the official entrance to age animation? http://age.tv Software introduction: 1. There will be many animations in the software, allowing everyone to see more complete comic content. 2. The animations here are completely free, allowing everyone to read comics online without spending money. 3. You can watch animations more smoothly in the software. The animations here have many

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

See all articles