【发布时间】:2014-09-10 00:45:18
【问题描述】:
有人可以介绍一种更有效或更正确的在 php 中分配变量的方法吗?
这是我的代码的 sn-p:
类 PurchaseController 扩展 BaseController {
/**
*Gets input form form
*
*@var input
*/
protected $input;
public function postPurchaseCheck()
{
$input = Input::all();
$this->input = $input;
如果表单提交按钮的值为“购买”,则使用 postPurhcase 方法
if (Input::get('buy')) {
$this->postPurchase();
}
elseif (Input::get('cart')) {
$this->postAddCart();
}
}
public function postAddCart()
{
//Add these items to cart
echo "Add these items to cart";
$memory = $this->input['memory'];
$color = $this->input['color'];
$size = $this->input['size'];
$price = $this->input['price'];
$battery = $this->input['battery'];
$accessories = $this->input['accessories'];
…………等等
}
public function postPurchase()
{
//Get prices, item id, etc and send user to checkout page
$memory = $this->input['memory'];
$color = $this->input['color'];
$size = $this->input['size'];
$price = $this->input['price'];
$battery = $this->input['battery'];
$accessories = $this->input['accessories'];
…………等等。我想知道是否有更快的方法在 php 中执行此操作而无需为每个方法重新分配变量 } }
【问题讨论】:
-
没有明显的理由让您以这种方式为每个输入创建局部变量。为什么不直接使用来自
$this->input的值?
标签: php variables variable-assignment