arvil
arvil

Reputation: 920

Is it possible to make a function/method return the result of another function if that function returns something?

Suppose you have a class that can be instantiated by a null value or an int variable. Now, you want to make most of it's methods to return null immediately if that instantiating variable is null otherwise each method will run its necessary logic, that class would look something like this:

function get_number_from_int($int) {
    if (1 === $int) {
        return 'one';
    }
    return 'dunno';
}

class Test {
    
    protected ?int $null_or_int = null;
    
    public function __construct($null_or_int) {
        $this->null_or_int = $null_or_int;
    }
    
    public function get_number_name() : ?string {
        
        if (null === $this->null_or_int) {
            return null;
        }
        
        return get_number_from_int($this->null_or_int);
        
    }
    
    public function is_it_one() : ?bool {
        
        if (null === $this->null_or_int) {
            return null;
        }
        
        return ($this->null_or_int === 1);
        
    }
    
}


var_dump((new Test(1))->get_number_name());
var_dump((new Test(1))->is_it_one());
var_dump((new Test(null))->get_number_name());
var_dump((new Test(null))->is_it_one());

Then it will return the following results as expected:

string(3) "one"
bool(true)
NULL
NULL

But notice that we have so many repeats of

if (null === $this->null_or_int) {
    return null;
}

on each of our method.

Is there a possible way that we can immediately bail out or return the result of another method if that method has a return value, say:

private function return_null_if_not_int() {
    if (null === $this->null_or_int) {
        return null;
    }
}

^ so this function will return null if it's validation condition is met, otherwise it won't return anything/void

so, ideally on each of our methods, instead of repeating that 3 line if-statement, we can simply:

public function get_number_name() : ?string {

    $this->return_null_if_not_int();

    return get_number_from_int($this->null_or_int);

}

and

public function is_it_one() : ?bool {
    
    $this->return_null_if_not_int();
    
    return ($this->null_or_int === 1);
    
}

^ so that if that validation method returns something, it will return that instead, otherwise, it will proceed with the rest of the logic of that method.

The above examples obvious won't work, though it runs that validation method, it will still proceed with processing the rest of the method's code.

Is there a more elegant way to do this, something that will reduce the amount of repeats?

Thank you!

Upvotes: 1

Views: 37

Answers (1)

Jared
Jared

Reputation: 1374

You could mark your methods as private and access them though the __call() magic method:

private function someMethod(){
  return 'always something usefull';
}
function __call( $method_name ){
  return (
    is_null($this->null_or_int)
    ? null
    : $this->$method_name()
  );
}

Note that this approach is usually considered as a bad practice and have some impact on performance.

Upvotes: 1

Related Questions