1
\$\begingroup\$

Is better to use $id_name ( Class A Method b() ) OR $id_name_2 ( Class A Method c())?

<?php

Class A { 

    function a(){
        $params = array(
            'id'=> 1,
            'name' => 'b',
        );

        $id_name = $this->b($params);
        $id_name_2 = $this->c($params);
    }

    function b($params){
        $id = $params['id'];
        $name = $params['name'];
        /*******

            Some code performing operations with $name and $id

        ********/

        $str = $id . $name; 
        return $str;
    }


    function c($params){

        /*******

            Some code performing operations with $params['name'] and $params['id']

        ********/

        $str = $params['name'] . $params['id'];

        return $str;
    }

}
\$\endgroup\$
1
  • \$\begingroup\$ Is $params being used at all inside of the "Code Performing Operations?" If not, both methods are nearly identical. \$\endgroup\$ – Kayla Mar 27 '14 at 2:42
1
\$\begingroup\$

Using method c will have an side-effect if $params is modified. This is because arrays are passed by reference, and so any changes to $params will be visible to the caller when the function returns. Use b if you need to manipulate id or name, unless you intend to return the modifications to the caller.

Example:

$params = array('id' => 1, 'name' => 'b');
$this->d($params);
echo $params['id']; // outputs 2
...

function d($params) {
    $params['id']++;
}
\$\endgroup\$
0
\$\begingroup\$

You want to be using method B. It will make for more readable code and will lead to less mystery bugs.

One cool PHP trick for extracting an assosiative array into variables is the Extract method.

$params = array(
    'id' => 1,
    'name' => 'foo'
);

extract($params);

echo $name . $id;
//"Foo1"

This makes more sense when you've got more elements, but I've always found it to be a clean way to express what you're trying to do.

The reverse is Compact which is great if you need to convert your values back to their original array form.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ My experience is that using extract can be a bit dangerous unless you are really sure of what you are doing and what the contents of the array are. Consider the fact that the old register_globals setting were doing an extract of $_POST, $_GET and others... and register_globals was one of the most horrible security leaks of PHP ever. \$\endgroup\$ – Simon Forsberg Mar 21 '14 at 12:38
  • \$\begingroup\$ I'm always sure what I'm doing ;) But you are definitely correct. I've always used extract on some kind of data model I wanted to operate on that had tons of properties. The arrays themselves were returned through class methods that I either wrote or thoroughly understood, so the safety was there. If you don't have this consistency with your input arrays, it will be safer to just assign them manually. \$\endgroup\$ – Will Mar 21 '14 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.