3

As this function can take unknown numbers of parameters:

function BulkParam(){
    return func_get_args();
}

A print_r will print only the values, but how i can retrieve the variable names as well as the array key? For example:

$d1 = "test data";
$d2 = "test data";
$d3 = "test data";

print_r(BulkParam($d1, $d2, $d3));

It will print this:

Array
(
    [0] => test data
    [1] => test data
    [2] => test data
)

But i want to have the variables name as the index name or key name of all arrays. Then the array would look like this:

Array
(
    [d1] => test data
    [d2] => test data
    [d3] => test data
)
rakibtg
  • 5,521
  • 11
  • 50
  • 73
  • 1
    You can't easily retrieve the variable names used when the function/method is called, and there is no business logic reason that you should need to – Mark Baker Aug 23 '14 at 10:42
  • Using debug_backtrace() will allow you to get that information, but that is purely a debugging aid, not a tool to use in working code – Mark Baker Aug 23 '14 at 10:43
  • 2
    And what would you expect from `print_r(BulkParam('test data 1', 'test data 2', 'test data 3'));` – Mark Baker Aug 23 '14 at 10:44

2 Answers2

3

I'm sure the PHP elite will have so many problems with this solution but it does work (I'm using PHP 5.6) and honestly I don't care since I've done hacks far worst than this for prototyping in many of my Java projects.

function args_with_keys( array $args, $class = null, $method = null, $includeOptional = false )
{
    if ( is_null( $class ) || is_null( $method ) )
    {
        $trace = debug_backtrace()[1];

        $class = $trace['class'];
        $method = $trace['function'];
    }

    $reflection = new \ReflectionMethod( $class, $method );

    if ( count( $args ) < $reflection->getNumberOfRequiredParameters() )
        throw new \RuntimeException( "Something went wrong! We had less than the required number of parameters." );

    foreach ( $reflection->getParameters() as $param )
    {
        if ( isset( $args[$param->getPosition()] ) )
        {
            $args[$param->getName()] = $args[$param->getPosition()];
            unset( $args[$param->getPosition()] );
        }
        else if ( $includeOptional && $param->isOptional() )
        {
            $args[$param->getName()] = $param->getDefaultValue();
        }
    }

    return $args;
}

Using the PHP Reflections API, we get all the method parameters and align them with their numeric indexes. (There might be a better way to do that.)

To use simply type:

print_r( args_with_keys( func_get_args() ) );

I also added the ability to optionally return the method's optional parameters and values. I'm sure this solution is far from perfect, so you're mileage may vary. Do keep in mind that while I did make it so providing the class and method was optional, I do highly suggest that you specify them if you're going anywhere near a production environment. And you should try to avoid using this in anything other than a prototype setup to begin with.

Specify the class and method with:

print_r( args_with_keys( func_get_args(), __CLASS__, __FUNCTION__ ) );
1

You can not. Variable names are not passed into functions. Variables are placeholders local to a specific algorithm, they are not data and they do not make sense in another scope and are not passed around. Pass an explicitly named associative array if you need key-value pairs:

bulkParam(['d1' => $d1, 'd2' => $d2, ...]);

A shortcut for this is:

bulkParam(compact('d1', 'd2'));

Then use an array:

function bulkParam(array $params) {
    foreach ($params as $key => $value) ...
}

As Mark mentions in the comments, sometimes you don't even have variables in the first place:

bulkParam('foo');
bulkParam(foo(bar(baz())));

Now what?

Or eventually you'll want to refactor your code and change variable names:

// old
$d1 = 'd1';
bulkParam($d1);

// new
$userName = 'd1';
bulkParam($userName);

Your application behaviour should not change just because you rename a variable.

deceze
  • 510,633
  • 85
  • 743
  • 889