One idea would be to set an internal flag to indicate success or failure and access it using another method, checking this flag in each method and not doing anything if it is set. For instance:.
class A { private $valid = true; public function check1() { if (!$this->valid) { return $this; } if (!) { $this->valid = false; } return $this; } public function check2() { if (!$this->valid) { return $this; } if (!) { $this->valid = false; } return $this; } public function isValid() { return $this->valid; } }
To minimize template validation in each function, you can also use the __call()
magic method. For instance:.
class A { private $valid; public function __call($name, $args) { if ($this->valid) { $this->valid = call_user_func_array("do" . $name, $args); } return $this; } private function docheck1() { return ; } private function docheck2() { return ; } public isValid() { return $this->valid; } }
Usage will be the same as above:
$a = new A(); if (!$a->check1()->check2()->isValid()) { echo "error"; }
source share