3

Is it possible to easily pass the individual items of an array to a method as parameters?

Like so:

Edit2:

I changed my code to show 'a solution', the problem with this solution is that it is deprecated since PHP 4.1 and removed in PHP 7.

<?php
    $builder = new Builder;
    $params = array('This is number: ', 4);

    echo call_user_method_array('add', $builder, $params);

    class Builder {
        public function add($string1, $number1) {
            return $string1 . $number1;
        }
    }
?>

Edit:

I made a mistake by using a simple example. The reason I am wondering whether this is possible is not for a function that returns a string like that. It is to create a MVC-like framework. The solution has to be all-round.

2
  • pass array and implod it, or save array element in some variable and the pass it to function Commented Apr 26, 2016 at 6:49
  • 2
    Try like this : public function add($params) { return implode('',$params); } Commented Apr 26, 2016 at 6:51

3 Answers 3

2

If you want to stick with your general solution, just use the proper non-deprecated method:

echo call_user_func_array([$builder, 'add'], $params);

Reference: callable

Demo: https://3v4l.org/4VFi6

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

Comments

2

Use implode() method.

<?php
    $builder = new Builder;
    $params = array('This is number: ', 4);

    echo $builder->add($params); //returns 'This is number: 4'

    class Builder {
        public function add($params) {
            return implode('', $params);
        }
    }
?>

Output

This is number: 4 

Check Online Demo : Click Here

OR

Pass 2 parameters $params[0],$params[1].

<?php
    $builder = new Builder;
    $params = array('This is number: ', 4);

    echo $builder->add($params[0],$params[1]); //returns 'This is number: 4'

    class Builder {
        public function add($string1 ,$number1) {

            return $string1 . $number1;
        }
    }
?>

Online Demo : Click Here

4 Comments

it should be return implode('',$params); since OP adds space in the array value itself
I edited my post, my example code is not the only use-case I have for this problem. And as such the solution needs to be all-round.
not clear. what you want exactly. Do you want to pass 2 parameters in function? what is your main issue?
Still facing issue? Please describe your issue in detail. @kgongonowdoe
0

Here is a more generic approach:

Just pass the array to the function and iterate through it by using a foreach-loop

<?php
    $builder = new Builder;
    $params = array('This is number: ', 4);

    $builder->add($params); //returns 'This is number: 4'

    class Builder {
        public function add($array) {
            foreach($array as $key => $value){
                //Do something with your values here if you want
                return $key . $value;
            }
        }
    }
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.