php链式操作实现四则链式运算
重点在于,返回$this指针,方便调用后者函数。
Operation.php
<?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 division($number) { $this->number /= $number; return $this; } public function get() { return $this->number; } }
index.php
require __DIR__ . '/IMooc/Operation.php'; $operation = new IMooc\Operation(10); $result = $operation->add(2)->decrease(2) ->multiply(3)->division(4) ->get(); var_dump($result);
执行结果
masaki@masaki-Inspiron:/var/www/imooc$ php index.phpfloat(7.5)
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23