Should I pass whole object as function parameter?

function, parameters, php

Solution

I believe the first option is the way to go.

function some($a) {
 var_dump($a->b);
 var_dump($a->d[1]);
}

some($a);

Because maybe over time you will need other parts of that object/array and you will have to modify the function and the calls where you make them. So `function some()` can be a global function and used in other ways, you can add later statements, modify the function without having to modify everywhere.

function some($a, $param1 = 1){
    if($param1 == 1){
        var_dump($a->c);
        var_dump($a->d[2]);
    } else {
        var_dump($a->b);
        var_dump($a->d[1]);
    }
}

Problem

Assume we have some object ``` $a = new stdClass; $a->b = 7; $a->c = 'o'; $a->d = array(1,2,3); ``` Also we have a function which doing something with some of properties of our object (for example `b` and `d[1]`). My question: Should my function accept whole object as a parameter like: ``` function some($a) { var_dump($a->b); var_dump($a->d[1]); } some($a); ``` or accept only certain fields which it's process like: ``` function some($b, $d) { var_dump($b); var_dump($d); } some($a->b, $a->d[1]); ``` Actually no matter is `$a` object or array.

Original source