Detailed explanation of PHP packaging concept

小云云
Release: 2023-03-22 21:08:01
Original
4820 people have browsed it

This article mainly shares with you a detailed explanation of the concept of PHP encapsulation, mainly in the form of text and code, hoping to help everyone.

/* 面向对象三大特征: 封装 继承 多态 */
Copy after login
Copy after login

// ===Code part 1===

class Human { public $money = 1000; }$lisi = new Human();echo $lisi->money,'
'; //1000// 改变一下money$lisi->money = 500;echo $lisi->money,'
'; //500
Copy after login
Copy after login
/* 然而在现实中,别人把你的钱减少了,显然是不合理的 所以我们要把钱设为私有的 */
Copy after login

// ===Code part 2===

class Human { private $money = 1000; public function showMoney() { return $this->money * 0.8; } }$lisi = new Human();/* 此时我们再调用或调用更改money时就会失败, 因为我们把money“封起来”了 //echo $lisi->money,'
'; //1000 但是,我们不能光封起来,还要能够使用才行 *//* 我们不能直接翻别人口袋有多少钱 但可以问别人,但是别人说的不一定是真的 */echo $lisi->showMoney(); //800
Copy after login
Copy after login
/* 把某些重要属性 封装起来 然后通过一个开放的接口来操作. 这就实现对属性的封装. */
Copy after login
Copy after login

/ / ===Code Part 3===

// 封装在方法上的体现class Human { private $money = 1000; private $bank = 2000; private function getBank($num) { $this->bank -= $num; return $num; } public function send($much) { if($much <= 1000) { $this->money -= $much; return $much; } else if($much <= $this->money + $this->bank) { $num = $much - $this->money; //计算从银行取了多少钱 $this->money += $this->getBank($num); //从银行取出钱,加到现金里 $this->money -= $much; //再把钱借给朋友 return $much; } else { $this->money -= $much; return $much; //最后若实在借不了这么多的话返回false //return false; } } public function showMoney() { return $this->money; } public function showBank() { return $this->bank; } }$lisi = new Human();$m = $lisi->send(300);if($m) { echo '借了',$m,'元
'; echo '还剩下',$lisi->showMoney(),'元
'; }// 再借2000元$m = $lisi->send(2000);if($m) { echo '借了',$m,'元
'; echo '还剩下',$lisi->showMoney(),'元
'; echo '银行还有',$lisi->showBank(),'元
'; }
Copy after login
Copy after login
/* 对于一个对象,对外界开放一个接口, 调用接口时,内部进行的操作,不需要让外界知道. 隐藏了内部的一些实现细节. 这是对方法的封装. */
Copy after login
Copy after login
/* 面向对象三大特征: 封装 继承 多态 */
Copy after login
Copy after login

// ===Code Part 1===

class Human { public $money = 1000; }$lisi = new Human();echo $lisi->money,'
'; //1000// 改变一下money$lisi->money = 500;echo $lisi->money,'
'; //500
Copy after login
Copy after login
/* 然而在现实中,别人把你的钱减少了,显然是不合理的 所以我们要把钱设为私有的 */
Copy after login



// = ==Code part 2===

class Human { private $money = 1000; public function showMoney() { return $this->money * 0.8; } }$lisi = new Human();/* 此时我们再调用或调用更改money时就会失败, 因为我们把money“封起来”了 //echo $lisi->money,'
'; //1000 但是,我们不能光封起来,还要能够使用才行 *//* 我们不能直接翻别人口袋有多少钱 但可以问别人,但是别人说的不一定是真的 */echo $lisi->showMoney(); //800
Copy after login
Copy after login
/* 把某些重要属性 封装起来 然后通过一个开放的接口来操作. 这就实现对属性的封装. */
Copy after login
Copy after login

// ===Code part 3===

// 封装在方法上的体现class Human { private $money = 1000; private $bank = 2000; private function getBank($num) { $this->bank -= $num; return $num; } public function send($much) { if($much <= 1000) { $this->money -= $much; return $much; } else if($much <= $this->money + $this->bank) { $num = $much - $this->money; //计算从银行取了多少钱 $this->money += $this->getBank($num); //从银行取出钱,加到现金里 $this->money -= $much; //再把钱借给朋友 return $much; } else { $this->money -= $much; return $much; //最后若实在借不了这么多的话返回false //return false; } } public function showMoney() { return $this->money; } public function showBank() { return $this->bank; } }$lisi = new Human();$m = $lisi->send(300);if($m) { echo '借了',$m,'元
'; echo '还剩下',$lisi->showMoney(),'元
'; }// 再借2000元$m = $lisi->send(2000);if($m) { echo '借了',$m,'元
'; echo '还剩下',$lisi->showMoney(),'元
'; echo '银行还有',$lisi->showBank(),'元
'; }
Copy after login
Copy after login
/* 对于一个对象,对外界开放一个接口, 调用接口时,内部进行的操作,不需要让外界知道. 隐藏了内部的一些实现细节. 这是对方法的封装. */
Copy after login
Copy after login

Related recommendations:

php for Introduction to inheritance, polymorphism, and encapsulation of objects

Detailed explanation of error handling encapsulation class in PHP

Sharing of implementation code of PHP communication data encapsulation class

The above is the detailed content of Detailed explanation of PHP packaging concept. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!