简体   繁体   English

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

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

Consider the following class 考虑以下课程

class myClass {

   private $model;

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

    public function find($id) {
        $this->model = ORMfind($id);
    }
}

How do I prevent 我该如何预防?

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

The problem isn't HOW to use the above code but how to make update() a method only callable after find(). 问题不是如何使用上面的代码,而是如何使update()方法只能在find()之后调用。

EDIT: I changed what my method does so it was more clearly understood that I need to do one method (find()) before another (update()) 编辑:我改变了我的方法所做的事情,因此我更清楚地知道我需要在另一个方法(find())之前做一个方法(find())

You could add a flag to your code like so: 您可以为代码添加一个标志,如下所示:

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;
  }

} }

Setting the flag $canUpdate will caution the update() method to react accordingly. 设置标志$canUpdate会提醒update()方法做出相应的反应。 If update() is called, you can throw an exception or exit out of the method if the flag is still 0. 如果调用update()则如果标志仍为0,则可以抛出异常或退出方法。

To prevent from returning null value by get : 要防止通过get返回null值:

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

 }

You can also create a construct: 您还可以创建一个构造:

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

and then give a value to your $value without using set() method: 然后在不使用set()方法的情况下为$value

 $myClass=new myClass(10);  

Outputting text, returning void, I think all of this is wrong. 输出文本,返回void,我认为所有这些都是错误的。 When you do not expect something to happen, you should throw an exception: 如果您不希望发生某些事情,您应该抛出异常:

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
    }
}

Now you can do: 现在你可以这样做:

$obj = new MyClass();

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

The proper way to make sure ->update() can only be called when the model has been initialized is to turn it into a dependency: 只有在初始化模型时才能调用->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');

Alternatively, if you have multiple find operations, you could introduce them as static constructor methods: 或者,如果您有多个查找操作,则可以将它们作为静态构造函数方法引入:

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');

Update 更新

Tackling your immediate problem can be done by a simple check: 通过简单的检查可以解决您的紧急问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM