How to return false when chaining methods

断了今生、忘了曾经 提交于 2021-02-07 08:16:24

问题


I have a validation class which uses method chaining. I would like to be able to do single checks with TRUE/FALSE like this:

if ($obj->checkSomething()) {}

But also chain methods like this:

if ($obj->checkSomething()->checkSomethingElse()) {}

The problem however is that if one method returns FALSE, it will not send back an object and thus breaks the method chaining which ends with this error:

Fatal error: Call to a member function checkSomething() on a non-object in ...

Do I have to pick either single method return calls or method chaining or is there a workaround?


回答1:


One idea would be to set an internal flag to indicate success or failure, and access it via another method, while checking that flag in each method and not doing anything if it's set. E.g.:

class A {
    private $valid = true;

    public function check1() {
        if (!$this->valid) {
            return $this;
        }
        if (!/* do actual checking here */) {
            $this->valid = false;
        }
        return $this;
    }
    public function check2() {
        if (!$this->valid) {
            return $this;
        }
        if (!/* do actual checking here */) {
            $this->valid = false;
        }
        return $this;
    }
    public function isValid() {
        return $this->valid;
    }
}

// usage:

$a = new A();

if (!$a->check1()->check2()->isValid()) {
    echo "error";
}

To minimize the boilerplate checking in each function, you could also use the magic method __call(). E.g.:

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 /* do actual checking here, return true or false */;
    }
    private function docheck2() {
        return /* do actual checking here, return true or false */;
    }
    public isValid() {
        return $this->valid;
    }    
}

The usage would be same as above:

$a = new A();

if (!$a->check1()->check2()->isValid()) {
    echo "error";
}



回答2:


I believe you're looking to have an instance evaluate as true/false based on the outcome of validation.

While some languages allow you to override the boolean value of an instance, php does not (except for casting to string, that is. See PHP's Magic Methods).

Also, the booleans page in the PHP Manual has a list of things that evaluate to false, but it doesn't give any method of overriding the behavior either.

That being said, I'd suggest going with JRL's idea, and construct a chain of validation rules, then 'execute' it with a function that returns the boolean needed in your if statement.




回答3:


You could wrap them up in a subclass, perhaps.

e.g. if you have

class Validate {
    public function checkSomething($data) {
        if ($data === $valid) {
            return true;
        } 
        return false;
    }
    public function checkSomethingElse($data) {
        if ($data === $valid) {
            return true;
        } 
        return false;
    }
}

You could do this:

class ValidateChain extends Validate {
    protected $valid = true;

    public function checkSomething($data) {
        if (false === parent::checkSomething($data)) {
            $this->valid = false;
        }
        return $this;
    }
    public function checkSomethingElse($data) {
        if (false === parent::checkSomethingElse($data)) {
            $this->valid = false;
        } 
        return $this;
    }
    public function getIsValid() {
        return $this->valid;
    }
}

$v = new ValidationChain();
$valid = $v->checkSomething()->checkSomethingElse()->getIsValid();

Quick and dirty, E&OE. And you'd probably need to add a way to find out which bits weren't valid, etc.



来源:https://stackoverflow.com/questions/7868750/how-to-return-false-when-chaining-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!