Home >Backend Development >PHP Tutorial >Detailed explanation of PHP packaging concept

Detailed explanation of PHP packaging concept

小云云
小云云Original
2018-03-31 11:26:474911browse

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.

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

// ===Code part 1===

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

// ===Code part 2===

class Human {
    private $money = 1000;    public function showMoney() {
        return $this->money * 0.8;
    }
}$lisi = new Human();/*
此时我们再调用或调用更改money时就会失败,
因为我们把money“封起来”了
//echo $lisi->money,&#39;<br >&#39;; //1000

但是,我们不能光封起来,还要能够使用才行
*//*
我们不能直接翻别人口袋有多少钱
但可以问别人,但是别人说的不一定是真的
*/echo $lisi->showMoney(); //800
/* 把某些重要属性 封装起来 然后通过一个开放的接口来操作. 这就实现对属性的封装. */

/ / ===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 &#39;借了&#39;,$m,&#39;元<br >&#39;;    echo &#39;还剩下&#39;,$lisi->showMoney(),&#39;元<br >&#39;;
}// 再借2000元$m = $lisi->send(2000);if($m) {    echo &#39;借了&#39;,$m,&#39;元<br >&#39;;    echo &#39;还剩下&#39;,$lisi->showMoney(),&#39;元<br >&#39;;    echo &#39;银行还有&#39;,$lisi->showBank(),&#39;元<br >&#39;;
}
/* 对于一个对象,对外界开放一个接口, 调用接口时,内部进行的操作,不需要让外界知道. 隐藏了内部的一些实现细节.
这是对方法的封装. */
/* 面向对象三大特征: 封装 继承 多态 */

// ===Code Part 1===

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



// = ==Code part 2===

class Human {
    private $money = 1000;    public function showMoney() {
        return $this->money * 0.8;
    }
}$lisi = new Human();/*
此时我们再调用或调用更改money时就会失败,
因为我们把money“封起来”了
//echo $lisi->money,&#39;<br >&#39;; //1000

但是,我们不能光封起来,还要能够使用才行
*//*
我们不能直接翻别人口袋有多少钱
但可以问别人,但是别人说的不一定是真的
*/echo $lisi->showMoney(); //800
/* 把某些重要属性 封装起来 然后通过一个开放的接口来操作. 这就实现对属性的封装. */

// ===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 &#39;借了&#39;,$m,&#39;元<br >&#39;;    echo &#39;还剩下&#39;,$lisi->showMoney(),&#39;元<br >&#39;;
}// 再借2000元$m = $lisi->send(2000);if($m) {    echo &#39;借了&#39;,$m,&#39;元<br >&#39;;    echo &#39;还剩下&#39;,$lisi->showMoney(),&#39;元<br >&#39;;    echo &#39;银行还有&#39;,$lisi->showBank(),&#39;元<br >&#39;;
}
/* 对于一个对象,对外界开放一个接口, 调用接口时,内部进行的操作,不需要让外界知道. 隐藏了内部的一些实现细节.
这是对方法的封装. */

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!

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