Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP方法链接:在允许链接其他方法之前调用一个方法_Php_Oop_Method Chaining - Fatal编程技术网

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

PHP方法链接:在允许链接其他方法之前调用一个方法,php,oop,method-chaining,Php,Oop,Method Chaining,考虑下面的类 class myClass { private $model; public function update($input) { return $this->model->update($input); } public function find($id) { $this->model = ORMfind($id); } } 我如何预防 $myClass = new myClass;

考虑下面的类

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())

以防止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);  

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

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,则可以抛出异常或退出方法。

输出文本,返回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');
或者,如果您有多个查找操作,可以将它们作为静态构造函数方法引入:

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

是的,不要这样滥用国家权力。如果你不去上课,这个问题就会消失。这通常是更好的处理方式。。。