本篇文章给大家带来的内容是关于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 pision($number) { $this->number /= $number; return $this; } public function get() { return $this->number; } }
require __DIR__ . '/IMooc/Operation.php'; $operation = new IMooc\Operation(10); $result = $operation->add(2)->decrease(2) ->multiply(3)->pision(4) ->get(); var_dump($result);
masaki@masaki-Inspiron:/var/www/imooc$ php index.phpfloat(7.5)
以上就是php如何使用链式操作实现四则运算?(代码)的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号