【问题标题】:PHP Method Chaining: Calling one method before allowing other methods to be chainedPHP 方法链接:在允许链接其他方法之前调用一个方法
【发布时间】:2013-08-30 03:13:24
【问题描述】:

考虑下面的类

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() 之后调用的方法。

编辑:我改变了我的方法的作用,所以更清楚地知道我需要在另一种方法(update())之前执行一种方法(find())

【问题讨论】:

  • 是的,不要那样滥用状态。如果您将状态排除在课堂之外,问题就会消失。这通常是更好的处理方式......

标签: php oop method-chaining


【解决方案1】:

您可以像这样在代码中添加一个标志:

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(),如果flag还是0,可以抛出异常或者退出方法。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      输出文本,返回 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);
      }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-11
          • 2012-05-09
          • 2017-01-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多