I will explain the question with a simple function accepting any number of function
function abc() {
$args = func_get_args();
//Now lets use the first parameter in something...... In this case a simple echo
echo $args[0];
//Lets remove this first parameter
unset($args[0]);
//Now I want to send the remaining arguments to different function, in the same way as it received
.. . ...... BUT NO IDEA HOW TO . ..................
//tried doing something like this, for a work around
$newargs = implode(",", $args);
//Call Another Function
anotherFUnction($newargs); //This function is however a constructor function of a class
// ^ This is regarded as one arguments, not mutliple arguments....
}
I hope the question is clear now, what is the work around for this situation?
Update
I forgot to mention that the next function I am calling is a constructor class of another class.
Something like
$newclass = new class($newarguments);
for simple function calls
use call_user_func_array, but do not implode the args, just pass the array of remaining args to call_user_func_array
call_user_func_array('anotherFunction', $args);
for object creation
use: ReflectionClass::newInstanceArgs
$refClass = new ReflectionClass('yourClassName');
$obj = $refClass->newInstanceArgs($yourConstructorArgs);
or: ReflectionClass::newinstance
$refClass = new ReflectionClass('yourClassName');
$obj = call_user_func_array(array($refClass, 'newInstance'), $yourConstructorArgs);
Related
I'm curious to know if there is some way to call a function using an associative array to declare the parameters.
For instance if I have this function:
function test($hello, $world) {
echo $hello . $world;
}
Is there some way to call it doing something like this?
call('test', array('hello' => 'First value', 'world' => 'Second value'));
I'm familiar with using call_user_func and call_user_func_array, but I'm looking for something more generic that I can use to call various methods when I don't know what parameters they are looking for ahead of time.
Edit:
The reason for this is to make a single interface for an API front end. I'm accepting JSON and converting that into an array. So, I'd like different methods to be called and pass the values from the JSON input into the methods.
Since I want to be able to call an assortment of different methods from this interface, I want a way to pass parameters to the functions without knowing what order they need to be in. I'm thinking using reflections will get me the results I'm looking for.
With PHP 5.4+, this works
function test($assocArr){
foreach( $assocArr as $key=>$value ){
echo $key . ' ' . $value . ' ';
}
}
test(['hello'=>'world', 'lorem'=>'ipsum']);
Check the php manual for call_user_func_array
Also, look up token operator (...). It is a way to use varargs with functions in PHP. You can declare something like this: -
function func( ...$params)
{
echo $params[0] . ',' . parama[1];
}
You can use this function internal in your functions func_get_args()
So, you can use it like this one:
function test() {
$arg_list = func_get_args();
echo $arg_list[0].' '.$arg_list[1];
}
test('hello', 'world');
The following should work ...
function test($hello, $world) {
echo $hello . $world;
}
$callback = 'test'; <-- lambdas also work here, BTW
$parameters = array('hello' => 'First value', 'world' => 'Second value');
$reflection = new ReflectionFunction($callback);
$new_parameters = array();
foreach ($reflection->getParameters() as $parameter) {
$new_parameters[] = $parameters[$parameter->name];
}
$parameters = $new_parameters;
call_user_func_array($callback, $parameters);
I want to call an object method+property with the content stored in a var ...
for example :
// setup the object
$xpath = new DOMXpath();
// setup the 'method'+'property' to call
$var1 = "query('something')->item(O)->nodeValue";
$return = $xpath->$var1();
Obviously, I make a mistake ... assuming that direct call is working, i.e.:
$return2 = $xpath->query('something')->item(0)->value;
echo "Return2 : ".$return2; //print okeedokee ...
How to pass args to query()? And how to add extra args to it?
I think you have to call
$return=$xpath->$var1;
Note : call_user_func is the function you need
Example :
alpha.php
class Alpha
{
public function getAlpha($arr_input)
{
echo "<pre>";
print_r($arr_input);
}
}
index.php
include_once 'alpha.php';
$post = array('one','two','three');
$obj_alpha = new Alpha();
call_user_func( array( $obj_alpha , 'getAlpha' ), $post ) ;
//here I call `getAlpha` function from object of class alpha (`$obj_alpha`)
with argument `$post`
//will print $post array
You can do this using eval():
$return = eval('return $xpath->' . $var1 .';');
However, using eval() with user-input is pretty much always a bad idea. So be careful there.
I know that it is possible to call a function with a variable number of parameters with call_user_func_array() found here -> http://php.net/manual/en/function.call-user-func-array.php . What I want to do is nearly identical, but instead of a function, I want to call a PHP class with a variable number of parameters in it's constructor.
It would work something like the below, but I won't know the number of parameters, so I won't know how to instantiate the class.
<?php
//The class name will be pulled dynamically from another source
$myClass = '\Some\Dynamically\Generated\Class';
//The parameters will also be pulled from another source, for simplicity I
//have used two parameters. There could be 0, 1, 2, N, ... parameters
$myParameters = array ('dynamicparam1', 'dynamicparam2');
//The instantiated class needs to be called with 0, 1, 2, N, ... parameters
//not just two parameters.
$myClassInstance = new $myClass($myParameters[0], $myParameters[1]);
You can do the following using ReflectionClass
$myClass = '\Some\Dynamically\Generated\a';
$myParameters = array ('dynamicparam1', 'dynamicparam2');
$reflection = new \ReflectionClass($myClass);
$myClassInstance = $reflection->newInstanceArgs($myParameters);
PHP manual: http://www.php.net/manual/en/reflectionclass.newinstanceargs.php
Edit:
In php 5.6 you can achieve this with Argument unpacking.
$myClass = '\Some\Dynamically\Generated\a';
$myParameters = ['dynamicparam1', 'dynamicparam2'];
$myClassInstance = new $myClass(...$myParameters);
I implement this approach a lot when function args are > 2, rather then end up with an Christmas list of arguments which must be in a specific order, I simply pass in an associative array. By passing in an associative array, I can check for necessary and optional args and handle missing values as needed. Something like:
class MyClass
{
protected $requiredArg1;
protected $optionalArg1;
public function __construct(array $options = array())
{
// Check for a necessary arg
if (!isset($options['requiredArg1'])) {
throw new Exception('Missing requiredArg1');
}
// Now I can just localize
$requiredArg1 = $options['requiredArg1'];
$optionalArg1 = (isset($options['optionalArg1'])) ? $options['optionalArg1'] : null;
// Now that you have localized args, do what you want
$this->requiredArg1 = $requiredArg1;
$this->optionalArg1 = $optionalArg1;
}
}
// Example call
$class = 'MyClass';
$array = array('requiredArg1' => 'Foo!', 'optionalArg1' => 'Bar!');
$instance = new $class($array);
var_dump($instance->getRequiredArg1());
var_dump($instance->getOptionalArg1());
I highly recommend using an associative array, however it is possible to use a 0-index array. You will have to be extremely careful when constructing the array and account for indices that have meaning, otherwise you will pass in an array with offset args and wreck havoc with your function.
You can do that using func_get_args().
class my_class {
function __construct( $first = NULL ) {
$params = func_get_args();
if( is_array( $first ) )
$params = $first;
// the $params array will contain the
// arguments passed to the child function
foreach( $params as $p )
echo "Param: $p\n";
}
}
function my_function() {
$instance = new my_class( func_get_args() );
}
echo "you can still create my_class instances like normal:";
$instance = new my_class( "one", "two", "three" );
echo "\n\n\n";
echo "but also through my_function:";
my_function( "one", "two", "three" );
Basically, you simply pass the result of func_get_args to the constructor of your class, and let it decide whether it is being called with an array of arguments from that function, or whether it is being called normally.
This code outputs
you can still create my_class instances like normal:
Param: one
Param: two
Param: three
but also through my_function:
Param: one
Param: two
Param: three
Hope that helps.
I've found here
Is there a call_user_func() equivalent to create a new class instance?
the example:
function createInstance($className, array $arguments = array())
{
if(class_exists($className)) {
return call_user_func_array(array(
new ReflectionClass($className), 'newInstance'),
$arguments);
}
return false;
}
But can somebody tell me if there is an example for classes with protected constructors?
I have a function variable like this...
$aName = "My Name";
The function need to pass in like this:
$sayHelloFunction = function sayHello($aName){
echo($aName);
}
Than, I have a method that responsible for execute the sayHelloFunction:
runningFunction($sayHelloFunction($aName));
in the runningFunction, it will have some condition to execute the function, but when I pass the "$sayHelloFunction($aName)" to runningFunction, it execute automatically, but I would like to pass the variable $aName as well, how can I achieve it? Thank you.
runningFunction($sayHelloFunction, $aName);
Simples.
You will have to pass the arguments separately. However, you could wrap them in an array so that you can pass them to runningFunction as a single argument, like this:
$printFunction = function($args) {
print $args['lastname'].', '.$args['firstname'];
};
function runningFunction($f, $a) {
$f($a);
}
$firstname = 'Bob';
$lastname = 'Smith';
$functionArguments = array(
'firstname' => $firstname,
'lastname' => $lastname
);
runningFunction($printFunction, $functionArguments);
If you want your dynamic functions to get "proper" arguments, then I see no way around something like this:
function runningFunction($f, $a) {
switch(count($a)) {
0: $f(); break;
1: $f($a[0]); break;
2: $f($a[0], $a[1]); break;
3: $f($a[0], $a[1], $a[2]); break;
// and so on
}
}
Pass the parameters as an array, and then use call_user_func_array() to call your function.
This way your runningFunction() will be absolutely abstract (as you requested), it can call any type of function, it's your responsibility to pass the right number of parameters.
function runningFunction ($callback, $parameters=array()) {
call_user_func_array($callback, $parameters);
}
runningFunction($sayHelloFunction, array($aName));
call_user_func_array()
as xconspirisist suggested pass $aName as a seperate parameter to the function.
Details on Variable Functions can be found on the PHP site.
Use an anonymous function when calling runningFunction
function runningFunction($func) {
$func();
}
runningFunction(function() {
$sayHelloFunction($aName));
// You can do more function calls here...
});
The below function generates error when a function contains referenced arguments eg:
function test(&$arg, &$arg2)
{
// some code
}
Now I can not use call_user_func_array for above function, it will generate an error.
How to solve this problem?
I do need to use call_user_func_array.
Also assume that i don't know beforehand whether they are passed by reference or passed by value.
Thanks
When storing your parameters in the array, make sure you are storing a reference to those parameters, it should work fine.
Ie:
call_user_func_array("test", array(¶m1, ¶m2));
A great workaround was posted on http://www.php.net/manual/de/function.call-user-func-array.php#91503
function executeHook($name, $type='hooks'){
$args = func_get_args();
array_shift($args);
array_shift($args);
//Rather stupid Hack for the call_user_func_array();
$Args = array();
foreach($args as $k => &$arg){
$Args[$k] = &$arg;
}
//End Hack
$hooks = &$this->$type;
if(!isset($hooks[$name])) return false;
$hook = $hooks[$name];
call_user_func_array($hook, $Args);
}
The actual hack is surrounded by comments.