Page 1 of 1
call_user_func() twice doesn't work?
Posted: Wed Jun 04, 2003 10:07 am
by lcidw
why doesn't this work?
page one
page two
Code: Select all
<?php
function action($value) {
return call_user_func('example',$value);
}
function example($value) {
call_user_func('example2',$value);
}
function example2($value) {
return $value;
}
?>
Posted: Wed Jun 04, 2003 10:20 am
by twigletmac
There's no return in this function:
Code: Select all
function example($value) {
call_user_func('example2',$value);
}
Mac
Posted: Wed Jun 04, 2003 10:27 am
by lcidw
oh
anyway, when do i need to use call_user_func() and when can i just call the function regulary, like action() ?
Posted: Wed Jun 04, 2003 10:36 am
by nielsene
You can call it regularly. Why do you think you can't?
You'll have to flip the order of function definations so that the function is defined before its called, but otherwise....
Posted: Thu Jun 05, 2003 2:58 am
by lcidw
but then again, why is there a call_user_func(), why did they create this?
Posted: Thu Jun 05, 2003 9:08 am
by nielsene
Reading over the manual, they don't list anything that only it can do. I would guess its there to provide a slightly more "formal" way of doing variable functions ie
Code: Select all
$functionName = "myfunc";
$functionName("foo");
or
Code: Select all
$functionName="myfunc";
call_user_func($myFunction,"foo");
The call_user_func method raises a visible flag that we're calling a different function based on our input that some poeple consider more readable. So its mainly a style issue. It also coul be useful for creating a closure system, but I doubt that's why its there.