php - Invalid argument supplied for foreach while loading functions through an array -
i got next code application:
public function generate_function_list($generated){ foreach($generated $method){ call_user_func($method); } } public function echotest($text){ echo '<p>' . $text . '</p>'; } and execute this:
$arrayx = array( formgenerator::echotest("test container 1"), formgenerator::echotest("test container 2"), formgenerator::echotest("test container 3"), formgenerator::echotest("test container 4") ); $nez->generate_function_list($arrayx); this output:
<p>testcontainer 1</p><p>testcontainer 2</p><p>testcontainer 3</p><p>testcontainer4</p> yes, can see output correct, executes function , params correctly, unfortunately below:
warning: invalid argument supplied foreach() in c:\appserv\www\test\testclassgenerator.php on line 2
i've been checking foreach within generate_function_list function, , see can't read functions beingness set inside, it's bit strange.
my intention phone call methods dinamically, using array, , giving timely params.
thanks!
an illustration of why array building incorrect:
function foo() { echo 'foo'; // immediate output of 'foo', no homecoming value } function bar() { homecoming 'bar'; // no output, homecoming 'bar' calling context } $foo = foo(); $bar = bar(); var_dump($foo); // outputs: null var_dump($bar); // outputs: string(3) "bar" $array = array( foo(), bar() ); var_dump($array); output:
array(2) { [0]=> null [1]=> string(3) "bar" } your echotest performs output. has no return call. function has no return gets assigned null value php when execution returns calling context.
so array, stated in dump output, array of nulls, 1 each echotest() phone call made within array. pass array generate_function_list(), iterate on nulls, , series of call_user_func(null) calls, pointless.
php arrays oop foreach
No comments:
Post a Comment