php - 从多个函数中访问数据
问题描述
在我需要在多个函数中访问 3 个计算的地方,有没有比使用函数更好的选择,例如:
public function shipping()
{
// Calculate shipping into $shipping
return $shipping;
}
public function tax()
{
// Calculate tax into $tax
return $tax;
}
public function total()
{
// Calculate total into $total
return $total;
}
然后调用:
$this->shipping();
我读过最好避免使用全局变量。
解决方案
您可以使用以下代码来减少函数调用
public function getProductCalculations($productId){
$return = array()
// Calculate total into $total
// Calculate tax into $tax
// Calculate shipping into $shipping
$return['tax'] = $tax;
$return['shipping'] = $shipping;
$return['total'] = $total;
return $return;
}
然后你可以得到如下给出的数据
$productCalc = $this->getProductCalculations($productId);
推荐阅读
- php - Laravel 5.6 应用程序中不存在方法链接?
- javascript - 在页面(视觉)加载后执行 Javascript alert()
- sql - 如何在选择语句中检查列值是否为空
- html - 在 R 中下载 HTML 数据
- html - 带有图像的 flexbox,固定大小
- c++ - 如何以数字方式处理 log(0)?
- python - 删除 .csv 中的所有行,除了第一个在列中具有重复单元格的行
- ruby-on-rails - 为什么当我回复评论时,我只能看到第一条评论的答案?
- nginx - meidawiki 和 nginx 中的好 URL 给出错误:FastCGI 在标准错误中发送:“主要脚本未知”同时从上游读取响应标头
- vba - VBA Access 2013 表单:如何刷新?