call_user_func() twice doesn't work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lcidw
Forum Commoner
Posts: 58
Joined: Mon Apr 28, 2003 8:55 am
Location: Netherlands

call_user_func() twice doesn't work?

Post by lcidw »

why doesn't this work?

page one

Code: Select all

<?php
action($value);
?>
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;
}
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There's no return in this function:

Code: Select all

function example($value) { 
    call_user_func('example2',$value); 
}
Mac
lcidw
Forum Commoner
Posts: 58
Joined: Mon Apr 28, 2003 8:55 am
Location: Netherlands

Post by lcidw »

oh :)

anyway, when do i need to use call_user_func() and when can i just call the function regulary, like action() ?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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....
lcidw
Forum Commoner
Posts: 58
Joined: Mon Apr 28, 2003 8:55 am
Location: Netherlands

Post by lcidw »

but then again, why is there a call_user_func(), why did they create this?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
Post Reply