views:

149

answers:

4

Hello one and all,

The reason I am asking this question is because I have landed my first real (yes, a paid office job - no more volunteering!) Web Development job about two months ago. I have a couple of associates in computer information systems (web development and programming). But as many of you know, what you learn in college and what you need in the job site can be very different and much more. I am definitely learning from my job - I recreated the entire framework we use from scratch in a MVC architecture - first time doing anything related to design patterns.

I was wondering what you would recommend as the best way to pass/return values around in OO PHP? Right now I have not implement any sort of standard, but I would like to create one before the size of the framework increases any more. I return arrays when more than 1 value needs to get return, and sometimes pass arrays or have multiple parameters. Is arrays the best way or is there a more efficient method, such as json? I like the idea of arrays in that to pass more values or less, you just need to change the array and not the function definition itself.

Thank you all, just trying to become a better developer.

EDIT: I'm sorry all, I thought I had accepted an answer for this question. My bad, very, very bad.

+2  A: 

You can return anything you want: a single value, an array or a reference (depending on the function needs). Just be consistent.

But please don't use JSON internally. It just produces unnecessary overhead.

Henrik P. Hessel
+5  A: 

How often do you run across a situation where you actually need multiple return values? I can't imagine it's that often.

And I don't mean a scenario where you are returning something that's expected to be an enumerable data collection of some sort (i.e., a query result), but where the returned array has no other meaning that to just hold two-or-more values.

One technique the PHP library itself uses is reference parameter, such as with preg_match(). The function itself returns a single value, a boolean, but optionally uses the supplied 3rd parameter to store the matched data. This is, in essence, a "second return value".

Definitely don't use a data interchange format like JSON. the purpose of these formats is to move data between disparate systems in an expected, parse-able way. In a single PHP execution you don't need that.

Peter Bailey
Great idea with the reference parameter, I never thought of that. What would you recommend for parameters? Just use multiple parameters or a mix of single parameters and arrays where needed? (An option parameter for instance - an array of n length options)
Robert DeBoer
+1  A: 

I also use arrays for returning multiple values, but in practice it doesn't happen very often. If it does, it's generally a sensible grouping of data, such as returning array('x'=>10,'y'=>10) from a function called getCoordinates(). If you find yourself doing lots of processing and returning wads of data in arrays from a lot of functions, there's probably some refactoring that can be done to put the work into smaller units.

That being said, you mentioned:

I like the idea of arrays in that to pass more values or less, you just need to change the array and not the function definition itself.

In that regard, another technique you might be interested in is using functions with variable numbers of arguments. It is perfectly acceptable to declare a function with no parameters:

function stuff() {
    //do some stuff
}

but call it with all the parameters you care to give it:

$x = stuff($var1, $var2, $var3, $var4);

By using func_get_args(), func_get_arg() (singular) and func_num_args() you can easily find/loop all the parameters that were passed. This works very well if you don't have specific parameters in mind, say for instance a sum() function:

function sum()
{
    $out = 0;
    for($i = 0; $i < $c = func_num_args(); $i++) {
        $out += func_get_arg($i);
    }
    return $out;
}

//echoes 35
echo sum(10,10,15);

Food for thought, maybe you'll find it useful.

zombat
A: 

The only thing I'm careful to avoid passing/returning arrays where the keys have "special" meaning. Example:

<?php
// Bad. Don't pass around arrays with 'special' keys
$personArray = array("eyeColor"=>"blue", "height"=>198, "weight"=>103, ...); 

?>

Code that uses an array like this is harder to refactor and debug. This type of structure is better represented as an object.

<?php

Interface Person {
    /**
     * @return string Color Name
     */
    public function getEyeColor();
    ...
}
?>

This interface provides a contract that the consuming code can rely on.

Other than that I can't think of any reason to limit yourself.


Note: to be clear, associative arrays are great for list data. like:

<?php
// Good array
$usStates = array("AL"=>"ALABAMA", "AK"="ALASKA", ... );
?>
Lance Rushing