0

I have the following code

public function create(){
  return self::checkQuantity();
  return self::checkSameLocation();
  return self::someAnotherFunction();
  return self::someMoreFunction();
  ..
  ..
  ..

}

public function checkQuantity(){
  if(someCondition){
     return [foo,bar];
  }
}


public function checkSameLocation(){
  if(someCondition){
     return [foo,bar];
  }
}

I would like to return function inside create() only if it function inside it returns something else continue executing the create() function.

Based on the example:

  • create() gets called
  • if checkQuantity returns nothing then continue with checkSameLocation() without returning checkQuantity() function

NOTE: There could be multiple function calls inside create() sometimes, I would like to avoid using if statement checks.

Rehan
  • 3,813
  • 7
  • 37
  • 58
  • Without doing if checks, because if there are 10 functions calls that would make it messy. – Rehan Nov 19 '19 at 16:21
  • Without `if` checks you can't check __if__ nothing returned from first function – u_mulder Nov 19 '19 at 16:22
  • Im not sure what it is your functions return, are these functions wrapped in a class? You cant check if nothing returned from a function because it will always return null. – Thomas Gregory Nov 19 '19 at 16:25
  • In terms of OOD, I guess a suitable solution would be [chain of responsibility](https://refactoring.guru/design-patterns/chain-of-responsibility/php/example#example-1) – Andrii Filenko Nov 19 '19 at 16:35
  • @AndriiFilenko thanks, I will look into it. – Rehan Nov 19 '19 at 16:40

5 Answers5

3
public function create(){
    return self::checkQuantity()
           ?? self::checkSameLocation()
           ?? self::someAnotherFunction(); //and so on
}

See Null coalescing operator

Weltschmerz
  • 2,166
  • 1
  • 15
  • 18
2
public function create(){
  $data = function1();
  if ($data) { 
    return $data;
  }

  $data = function2();
  if ($data) { 
    return $data;
  }

  $data = function3();
  if ($data) { 
    return $data;
  }

  // ...

  return function42();
}

This can lead to another solution like:

public function create(){
  $functions = ['func1', 'func2', 'func3',]; // etc
  foreach ($functions as $func) {
    $data = $func();
    if ($data) { 
      return $data;
    }
  }
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
2

Something like this:

(in this case, I've moved all your check code into polymorphic classes; the same could be achieved with methods in the current class per your question, but I think it's neater this way)

$checks = [new checkClass1, new checkClass2, new checkClass3, ...];

foreach ($checks as $check) {
    $result = $check->runCheck();
    if ($result) {
        return $result;
    }
}
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Thanks for the answer. But coalesce operator is much cleaner approaches for my use case. – Rehan Nov 19 '19 at 16:33
0

You need an if and a temporary variable.

public function create(){
  $a = self::checkQuantity();
  if(empty($a)) { 
    return self::checkSameLocation();
  } else {
    return $a;
  }
}

public function checkQuantity(){
  if(someCondition){
     return [foo,bar];
  }
}


public function checkSameLocation(){
  if(someCondition){
     return [foo,bar];
  }
}
Adder
  • 5,708
  • 1
  • 28
  • 56
0

This looks like the perfect use case for the null coalescing operator

public function create(){
  return self::checkQuantity() ?? self::checkSameLocation() ?? self::someAnotherFunction() ?? return self::someMoreFunction()...
}

You can chain as many function calls as you want. That line will return the first value that's not null. Since returning nothing from a function effectively equals returning a null value, it'll work as you want.

José A. Zapata
  • 1,187
  • 1
  • 6
  • 12