本篇文章给大家带来的内容是关于php如何使用链式操作实现四则运算?(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
重点在于,返回$this指针,方便调用后者函数。
Operation.php
使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。
<?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
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免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










