php如何使用鏈?zhǔn)讲僮鲗?shí)現(xiàn)四則運(yùn)算?(代碼)
來源:不言
發(fā)布時(shí)間:2019-01-11 10:54:46
閱讀量:939
本篇文章給大家?guī)淼膬?nèi)容是關(guān)于php如何使用鏈?zhǔn)讲僮鲗?shí)現(xiàn)四則運(yùn)算?(代碼),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對你有所幫助。
重點(diǎn)在于,返回$this指針,方便調(diào)用后者函數(shù)。
Operation.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php
namespace IMooc;
class Operation
{
protected $number = 0;
public function __construct($number)
{
$this->number = $number;
}
public function add($number)
{
$this->number += $number;
return $this;
}
public function decrease($number)
{
$this->number -= $number;
return $this;
}
public function multiply($number)
{
$this->number *= $number;
return $this;
}
public function pision($number)
{
$this->number /= $number;
return $this;
}
public function get()
{
return $this->number;
}
}
|
index.php
1 2 3 4 5 6 | require __DIR__ . '/IMooc/Operation.php';
$operation = new IMooc\Operation(10);
$result = $operation->add(2)->decrease(2)
->multiply(3)->pision(4)
->get();
var_dump($result);
|
執(zhí)行結(jié)果
masaki@masaki-Inspiron:/var/www/imooc$ php index.php
float(7.5)