<?php
function register_template(){
print_r(func_get_args());
# the result was an array ( [0] => my template [1] => screenshot.png [2] => nice template .. )
}
register_template( # unkown number of arguments
$name = "my template",
$screenshot = "screenshot.png",
$description = "nice template .. "
)
?>
BUT , I want the result array as $key => $value form , $key represents the parameter name.
PHP does not support an arbitrary number of named parameters. You either decide on a fixed number of parameters and their names in the function declaration or you can only get values.
The usual way around this is to use an array:
function register_template($args) {
// use $args
}
register_template(array('name' => 'my template', ...));
Wanted to do the same thing and wasn't completely satisfied with the answers already given....
Try adding this into your function ->
$reflector = new ReflectionClass(__CLASS__);
$parameters = $reflector->getMethod(__FUNCTION__)->getParameters();
$args = array();
foreach($parameters as $parameter)
{
$args[$parameter->name] = ${$parameter->name};
}
print_r($args);
I haven't thought about trying to make this it's own function yet that you can just call, but might be able to...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With