is there any way to get list of variables that were defined in function, or list of all accessible variables in a function?
There is get_defined_vars():
This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
it has to be called within each function you want to analyze, though.
http://php.net/manual/en/function.get-defined-vars.php
Documentation indeed ^^
func_num_args — Returns the number of arguments passed to the function
func_get_arg — Return an item from the argument list
func_get_args — Returns an array comprising a function's argument list
Related
Let's assume we have this function
<?php
function run($callback)
{
$callback($some, $params);
}
How to call it?
run($someObject->callback);
or rather
run([$someObject, 'callback']);
The first one seems better to me especially because of code suggestion but in documentation is used array notation.
Why is first one worse than second one?
The array notation is better because the arrow notation doesn't work. Functions in PHP aren't first class objects which can be passed around. Whenever you "pass a function", you must pass it by name; i.e. only its name. To pass an object method, you need to pass the object and the method name using the callable pseudo type notation.
Could someone please explain what is going on here in this code?
I can see it is an array called b accessing an element with the key 'c', but the stuff in the brackets? I don't know what is going on here.
$a=$b['c']($d,$e,$f);
$b['c'] must be a function name.
try to print it, you'll see.
$a=$b['c']($d,$e,$f);
calls that function passing $d, $e and $f arguments to it.
Try :
<?php
$func = 'var_dump';
$foo = array(1,2,3);
$func($foo)
It looks like you are looking at a variable function.
The expression above first evaluates the associative array $a=$b['c'] and then calls the function with that name, passing the arguments $d,$e,$f.
From the description:
PHP supports the concept of variable functions. This means that if a
variable name has parentheses appended to it, PHP will look for a
function with the same name as whatever the variable evaluates to, and
will attempt to execute it. Among other things, this can be used to
implement callbacks, function tables, and so forth.
It seems element at key c is expected to be a function.
In PHP5, you can use this kind of short notations. The function will be called with parameters d e and f.
Is there a PhP function that can count the number of parameters passed to another function?
for example
function tobeCounted ("numberOne","numberTwo","numberThree")
{
//do something with the parameters
}
a function that will count the number of parameters passed to the function above then return three or the indexes of the parameters or parameters themseleves.
If you use func_num_args() then you get the amount of arguments, rather than func_get_args that gets you the array of arguments.
func_get_args
You need these:
func_num_args — Returns the number of arguments passed to the function
func_get_args — Returns an array comprising a function's argument list
From here:
http://www.php.net/manual/en/function.func-num-args.php
Or more directly: func_num_args
func_num_args()
Use this to get the number of parameters passed
OK, I know it sounds weird but I need to make a function that will receive two parameters the first one is a string and the second an array (containing strings).
This function will then call sprintf. My first parameter will be $format and my array will correspond to the various $args.
How can I achieve this (if possible)?
Thanks!
Well you want the vsprintf() function.
Like Orbling answered, for this particular case you need vsprintf.
But, in a generic case, to call a function with variable number of parameters, you can use func_get_args() inside the function which you desire to accept multiple (any number of variable arguments). This function (when called inside your function) returns an array containing all the parameters passed while calling your function.
Is there a function and/or object and/or extension in PHP that will let you view all the variables defined in the current scope? Something like:
var_export($GLOBALS)
but only showing variables in the current symbol table.
get_defined_vars
This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
get_defined_vars() does exactly what you want.
This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
>>> function test($foo) { print_r(get_defined_vars()); }
>>> test('bar');
Array
(
[foo] => bar
)