0

Below is a fragment of one of my functions, but I figured this seemed redundant and thus wanted to avoid it.

 function cellMaker($cell){
        $label= $cell['label'];
        $type= $cell['type'];
        $return= $cell['return'];
        $size= $cell['size'];
        $name= $cell['name'];
        $value= $cell['value'];
         ........

The reason I am doing this is to avoid having to fill in nulls with the function if I only need to pass two of the parameters, like just label and type and value. That would look like cellMaker('how?', 'text' null, null, null, 'because');

Rather I only would need to do cellMaker(["label" => "how?", "type"=> "text", "value" => "because"]) which saves me from having to remember the order the variables are defined in the function and from having to deal with unnecessary variables. However I also do not want to have to type $cell['variable'] rather than $variable each time.

Is there a way to automatically assign all variables of an object to function variables of the same name?

13
  • 2
    I don't think I got what you are trying to achieve. But take a look at extract function. Commented Jun 27, 2016 at 15:29
  • That is perfect, post as answer and I'll accept. Commented Jun 27, 2016 at 15:32
  • Though extract function is the right answer. I would not advise you to use it as you'll end up with bunch of variable that just appeared from nowhere. That would make debugging and support pain. Commented Jun 27, 2016 at 15:34
  • 1
    What is wrong with using $cell['label'] as is? Commented Jun 27, 2016 at 15:41
  • 1
    @JasonBasanese Your code, do what you want. You got an answer. Just accept it. People gave you warning that it is a bad idea, that is it. Commented Jun 27, 2016 at 15:46

1 Answer 1

1

You may use extract function to get separate variables from an array.

$array = ["label" => "how?", "type"=> "text", "value" => "because"];
extract($array);

This will give you three variables named after keys in the array containing corresponding values. Be warned though. It may become rather unpredictable. You wouldn't know for sure what kind of keys there may be in the input array.

Sign up to request clarification or add additional context in comments.

5 Comments

But actually extract function may become pretty unpredictable.
I would put that warning in big letters in the answer itself
How can it become unpredicatable?
You can actually pass any kind of array there. With any kind of keys from some other part of your code base. If you use objects you will be sure about the properties it contains. And it will actually save you some memory because PHP creates another array every time you pass it to a function. And with the object you will only send a reference to it.
@JasonBasanese Well, one thing if you had variable with same name right before you call extract you going to loose it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.