note 93869 added to function.call-user-func
| From: | Nitrogen at osu1 dot php dot net | Date: | Sun, 04 Oct 2009 23:08:58 +0000 |
| Subject: | note 93869 added to function.call-user-func | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161267@lists.php.net to get a copy of this message | ||
A good use for call_user_func(); is for recursive functions.
If you're distributing code, you will often come across users who will rename functions and
break the code..
Use this: call_user_func(__FUNCTION__, ... ); inside a function to call itself with whatever
parameters you want.
<?php
// example, an extremely simplified factorial calculator..
// it's quite obvious when someone renames the function, it'll spit out an error because
it wants to call itself.
function Factorial($i=1) {
return($i==1?1:$i*Factorial($i-1));
}
// you can give this function whatever name you want, it'll always work, of course if you
initially call it using the name you gave it.
function qwertyuiop($i=1) {
return($i==1?1:$i*call_user_func(__FUNCTION__,$i-1));
}
?>
Just that I didn't see any reference to recursive functions when user_call_func(); really
helps.
----
Server IP: 195.222.16.155
Probable Submitter: ::ffff:81.132.224.11
----
Manual Page -- http://www.php.net/manual/en/function.call-user-func.php
Edit -- https://master.php.net/note/edit/93869
Del: integrated -- https://master.php.net/note/delete/93869/integrated
Del: useless -- https://master.php.net/note/delete/93869/useless
Del: bad code -- https://master.php.net/note/delete/93869/bad+code
Del: spam -- https://master.php.net/note/delete/93869/spam
Del: non-english -- https://master.php.net/note/delete/93869/non-english
Del: in docs -- https://master.php.net/note/delete/93869/in+docs
Del: other reasons-- https://master.php.net/note/delete/93869
Reject -- https://master.php.net/note/reject/93869
Search -- https://master.php.net/manage/user-notes.php