PHP方法链接:在允许其他方法链接之前调用一个方法


PHP Method Chaining: Calling one method before allowing other methods to be chained

考虑以下类

class myClass {
   private $model;
    public function update($input) {
        return $this->model->update($input);
    }
    public function find($id) {
        $this->model = ORMfind($id);
    }
}

如何防止

$myClass = new myClass;
$myClass->update($input);

问题不在于如何使用上面的代码,而在于如何使update()成为一个只能在find()之后调用的方法。

编辑:我改变了我的方法,这样我就更清楚地知道我需要先做一个方法(find()),然后再做另一个(update())

您可以在代码中添加一个标志,如下所示:

class myClass {
  private $model;
  private $canUpdate = 0;
  public function update($input) {
    if ($canUpdate === 0) return; // or throw an exception here
    return $this->model->update($input);
  }
  public function find($id) {
    $this->model = ORMfind($id);
    $canUpdate = 1;
  }

}

设置标志$canUpdate将提醒update()方法做出相应的反应。如果调用了update(),则可以抛出异常,或者如果标志仍然为0,则可以退出该方法。

通过get:防止返回空值

public function get() {
    if (isset($this->value)) return $this->value;
    else echo "please give me a value ";
 }

您也可以创建一个构造:

 function __construct($val){
    $this->value=$val;  
 } 

然后在不使用set()方法的情况下给$value一个值:

 $myClass=new myClass(10);  

输出文本,返回void,我认为所有这些都是错误的。当你不希望发生什么事情时,你应该抛出一个异常:

class MyClass {
    private $canUpdate = false;
    public function find($id) {
        // some code...
        $this->canUpdate = true;
    }
    public function canUpdate() {
        return $this->canUpdate;
    }
    private function testCanUpdate() {
        if (!$this->canUpdate()) {
            throw new Exception('You cannot update');
        }
    }
    public function update($inpjut) {
        $this->testCanUpdate();
        // ... some code
    }
}

现在你可以做:

$obj = new MyClass();
try {
    $obj->update($input);
} catch (Exception $e) {
    $obj->find($id);
    $obj->update($input);
}

确保->update()只能在模型初始化时调用的正确方法是将其转换为依赖项:

class myClass 
{
    private $model;
    public function __construct($id)
    {
        $this->model = ORMfind($id);
    }
    public function update($input) {
        return $this->model->update($input);
    }
}
$x = new myClass('123');

或者,如果您有多个find操作,您可以将它们作为静态构造函数方法引入:

class myClass 
{
    private $model;
    private function __construct($model)
    {
        $this->model = $model;
    }
    public function update($input) {
        return $this->model->update($input);
    }
    public static function find($id)
    {
        return new self(ORMfind($id));
    }
}
$x = myClass::find('123');

更新

解决眼前的问题可以通过一个简单的检查来完成:

    public function update($input) {
        return $this->model ? $this->model->update($input) : null;
    }