Ragas
Ragas

Reputation: 3205

How to automatically call a another method in a class with result of the called method when a method is called?

I have a class with few methods like this..

class ClassA
{
    public functon funcA(arguments) : bool
    {
        // return true or false
    }

    public functon funcB(arguments) : bool
    {
        // return true or false
    }


    private function ifFalse(){ // do something }
}

What i want to do is call ifFalse() method automatically when funcA() or funcB() are called and they return false.

How can I achive this? I am using php 7 by the way.

Upvotes: 0

Views: 50

Answers (1)

dave
dave

Reputation: 64657

You can just call the function inside the other function:

$ret = false;
// return true or false
if (!$ret) $this->ifFalse();
return $ret;

If you want it more "magic", you could do:

public function __call($name, $arguments)
{
    switch($name) {
        case 'funcA':
        case 'funcB':
            $value = $this->$name(...$arguments);
            if (!$value) $this->ifFalse();
            return $value;
        break;
    }
}

private function funcA(arguments) : bool
{
    // return true or false
}

private function funcB(arguments) : bool
{
    // return true or false
}

Upvotes: 1

Related Questions