9

What would be recommended from a pure coding best practices perspective to adopt as a standard for medium-large developer teams?

Return a sequential array:

function get_results($filter) {
    $query = "SELECT SQL_CALC_FOUND_ROWS, * FROM ...";

    $results = ...
    $total = ...

    return array($results, $total);
}

Return an associative array:

function get_results($filter) {
    $query = "SELECT SQL_CALC_FOUND_ROWS, * FROM ...";

    $results = ...
    $total = ...

    return array(
        'resuts' => $results, 
        'total' => $total
    );
}

Return single result and assign the second by reference (?!):

function get_results($filter, &$count = null) {
    $query = "SELECT SQL_CALC_FOUND_ROWS, * FROM ...";

    $results = ...
    $total = ...

    $count = $total;
    return $results;
}

Feel free to suggest any other approach.

Emanuel Lainas
  • 152
  • 2
  • 11
  • Not sure, but if you look at PHP preg_match* they return a count and populate referenced match array. – AbraCadaver Jun 16 '15 at 15:35
  • I would never determine such a convention within my team. That is horrible. There are millions of other things that you would also have to determine. BTW you can ask the team regarding to what they prefer. – B001ᛦ Jun 16 '15 at 15:35
  • 2
    Personally, I love associative arrays, they are so easy to convert to object and from object. – Jose Manuel Abarca Rodríguez Jun 16 '15 at 15:35
  • But... What is the relation between $count the actual number of results? If they are the same then no need to return count as it can be determined from counting the results. – AbraCadaver Jun 16 '15 at 15:37
  • 1
    Isn't this question more appropriate on the Code Review site? – Rob Tillie Jun 16 '15 at 15:39
  • Just like @JoseManuelAbarcaRodríguez i prefer associative arrays. You can also `json_encode()` and send them to the client side to work with the javascript – Louis Loudog Trottier Jun 16 '15 at 15:42
  • 1
    @RobTillie: Hardly. It looks mostly hypothetical and has no context behind it. It's also more of a "polling" question, which isn't what CR is about. – Jamal Jun 16 '15 at 15:47
  • @AbraCadaver: assume $count is the not the number of $results, but the total items without applying the filter. Or anything else that for the sake of performance/redundancy needs to be calculated in the same function (the above was a simple example). – Emanuel Lainas Jun 16 '15 at 15:48
  • @bub: I understand that the importance of this is quite low and it all boils down to preference, but surely some things might be slightly easier to work with than others in terms of readability, how they get documented etc. – Emanuel Lainas Jun 16 '15 at 15:52

1 Answers1

7

From the PHP documentation:

A function can not return multiple values, but similar results can be obtained by returning an array.

All of these seem like good practices given what the documentation states. A comment in the documentation reveals one other way: using list(). See the example below:

function fn($a, $b)
{
   # complex stuff

    return array(
      $a * $b,
      $a + $b,
   );
}

list($product, $sum) = fn(3, 4);

echo $product; # prints 12
echo $sum; # prints 7
pkshultz
  • 409
  • 1
  • 3
  • 16