to brooklynphil at hotmail dot com:
Your post is misleading, namely the 3rd and last speedtest you describe is an unfair comparison:
<?php
is_callable('test','test');
?>
The value of the 2nd parameter $syntax_only is 'test' and this evaluates to boolean true. In other words, this is exactly the same as calling the function like this:
<?php
is_callable('test',true);
?>
Of course this will be faster because is_callable only does a very basic syntaxcheck. From the documentation: 'It will only reject simple variables that are not strings, or an array that does not have a valid structure to be used as a callback.'
If you omit this erroneous 3rd test, then according to your examples function_exists is 2 to 4 times faster.
I hope you can see that loop-testing functions is not so simple. :)
rtfm
function_exists
(PHP 4, PHP 5)
function_exists — 指定した関数が定義されている場合に TRUE を返す
説明
bool function_exists ( string $function_name )組み込みの内部関数およびユーザ定義関数の中から、 function_name で指定した名前の関数を探します。
パラメータ
- function_name
関数名を表す文字列。
返り値
function_name が存在し、関数である場合に TRUE、 それ以外の場合に FALSE を返します。
注意: この関数は、 include_once() や echo() のような言語構造については FALSE を返します。
例
例 735. function_exists() の例
<?php
if (function_exists('imap_open')) {
echo "IMAP 関数が利用可能です。<br />\n";
} else {
echo "IMAP 関数は利用できません。<br />\n";
}
?>
注意
注意: ある関数がそれ自体設定やコンパイルオプションの問題で使用できない 場合でもその関数の名前が存在する可能性があることに注意してください (例としては image 関数などがあります)。
参考
| method_exists() |
| is_callable() |
| get_defined_functions() |
function_exists
dieter at edarta dot be
16-Feb-2007 08:09
16-Feb-2007 08:09
eddiec at stararcher dot com
10-Feb-2007 02:58
10-Feb-2007 02:58
Note that function_exists will return TRUE in the following situation, presumably because the function "testfunc" was defined when the script was PARSED/ITERPRETED, before the function_exists call was made at RUNTIME:
<?
if (function_exists('testfunc')) return;
function testfunc() { }
?>
So, this construction is not useful for preventing testfunc from being multiply defined if the script is muliply included or required.
However, the following construction DOES work to prevent multiple defines of testfunc:
<?
if (!function_exists('testfunc')) {
function testfunc() { }
}
?>
CONTRAST this with similar uses of defined() which is completely runtime evaluated. These both work:
<?
if (defined('testfunc_defined')) return;
define('testfunc_defined', 1);
function testfunc() { }
?>
AND...
<?
if (!defined('testfunc_defined')) {
define('testfunc_defined', 1);
function testfunc() { }
}
brooklynphil at hotmail dot com
31-Jan-2007 04:02
31-Jan-2007 04:02
to bob at thethirdshift dot net
regarding is_callable vs function_exists.
using your code
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.0443360805511 seconds
Did 10000 function_exists in 0.0111110210419 seconds
then we replace
is_callable(array('test','test'));
with
$callarray = array('test','test'); // place this outside for-loop
is_callable($callarray);
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.0314660072327 seconds
Did 10000 function_exists in 0.0120670795441 seconds
then we replace
is_callable(array('test','test'));
with
is_callable('test','test');
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.00991606712341 seconds
Did 10000 function_exists in 0.0113790035248 seconds
I hope you can see that loop-testing functions is not so simple. :)
Dan
18-Jul-2006 12:49
18-Jul-2006 12:49
I would like to comment on the following post:
A note of caution: function_exists() appears to be case-insensitive (at least as of PHP 4.3.8). e.g.:
<?php
function MyCasedFunction() {
return true;
}
// Will return true, even though casing is "wrong"
if (function_exists("mYcAsEdFuNcTiOn"))
echo "I see it!";
?>
I believe that function calls itself are case insensitve, so this function is returning a valid truth. PHP doesn't care about cases.
andi at splitbrain dot org
07-Jul-2006 07:48
07-Jul-2006 07:48
function_exists will return false for functions disabled with the disable_functions ini directive. However those functions are still declared so trying to define them yourself will fail.
<?
if(!function_exists('readfile')){
function readfile($file){
$handle=@fopen($cache,"r");
echo @fread($handle,filesize($file));
@fclose($handle);
}
}
?>
The above will issue a "Cannot redeclare readfile()" fatal error if readfile was disabled with disable_functions.
chad 0x40 herballure 0x2e com
20-May-2006 02:06
20-May-2006 02:06
In-reply-to: neelam_ab2003 at yahoo dot co dot in
See the functions section of the manual, http://us2.php.net/manual/en/language.functions.php - once you call B() then function C will exist, and likewise for calling C to create D.
neelam_ab2003 at yahoo dot co dot in
11-May-2006 04:06
11-May-2006 04:06
<?php
/*PHP doesn't Support nested functions. I have tried following in PHP_VERSION - 5.1.2*/
function A(){}
function B(){
function C(){
function D(){}
}
}
IsFunctionExist('A');
IsFunctionExist('B');
IsFunctionExist('C');
IsFunctionExist('D');
function IsFunctionExist($funcName){
echo function_exists($funcName)?" $funcName exist <br>":" $funcName doesn't exist <br>";
}
?>
/*O U T P U T
A exist
B exist
C doesn't exist
D doesn't exist
*/
chaumo
16-Jul-2005 09:46
16-Jul-2005 09:46
to avoid direct calls this can be better than function_exists
in the parent file:
<?php
define("IN_MODULE",true);
?>
and in the target file:
<?php
if(!defined("IN_MODULE")) die("Can't access the file directly");
?>
fili at fili dot nl
09-Jun-2005 01:24
09-Jun-2005 01:24
To prevent direct calls to included files i use the following technique.
In the main file create an empty function with a random name. Like so:
<?php
function hjudejdjiwe() { return true; }
?>
Then check for the existence of this function within your include:
<?php
if (!function_exists('hjudejdjiwe')) { die('!'); }
?>
Simple but effective.
codeslinger at compsalot dot com
02-Feb-2005 11:11
02-Feb-2005 11:11
case-insensitive is by design
see http://www.php.net/manual/en/language.functions.php
"Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration."
PHP is not C, though the similarities can be confusing.
dark dot ryder at gmail dot com
07-Oct-2004 08:49
07-Oct-2004 08:49
A note of caution: function_exists() appears to be case-insensitive (at least as of PHP 4.3.8). e.g.:
<?php
function MyCasedFunction() {
return true;
}
// Will return true, even though casing is "wrong"
if (function_exists("mYcAsEdFuNcTiOn"))
echo "I see it!";
?>
bob at thethirdshift dot net
24-Jun-2004 01:55
24-Jun-2004 01:55
I, too, was wondering whether is_callable or function exists is faster when checking class methods. So, I setup the following test:
<?php
function doTimes($start, $end)
{
$start_time = explode (" ", $start);
$start_time = $start_time[1] + $start_time[0];
$end_time = explode (" ", $end);
$end_time = $end_time[1] + $end_time[0];
$time = $end_time - $start_time;
return $time;
}
class test
{
function test()
{
return true;
}
}
$callableIsTrue = false;
$startIsCallable = microtime();
for($i = 0; $i < 10000; $i++)
{
if(is_callable(array('test', 'test'))) { $callableIsTrue = true; }
}
$endIsCallable = microtime();
$existsIsTrue = false;
$startExists = microtime();
for($i = 0; $i < 10000; $i++)
{
if(function_exists('test::test')) { $existsIsTrue = true; }
}
$endExists = microtime();
$timeIsCallable = doTimes($startIsCallable, $endIsCallable);
$timeExists = doTimes($startExists, $endExists);
echo "<b>is_callable = ".($callableIsTrue ? "TRUE" : "FALSE")."</b>, \n";
echo "<b>function_exists = ".($existsIsTrue ? "TRUE" : "FALSE")."</b><br>\n";
echo "<br>Did 10000 is_callables in ".$timeIsCallable." seconds";
echo "<br>Did 10000 function_exists in ".$timeExists." seconds";
?>
This gives the output :
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.0640790462494 seconds
Did 10000 function_exists in 0.0304429531097 seconds
So the fact that function_exists is twice as fast is slightly over shadowed by the fact that it doesn't work on class methods, at least not as far as I can tell.
ckrack at i-z dot de
10-Mar-2004 05:22
10-Mar-2004 05:22
i was wondering whether is_callable or function exists is faster when checking class methods.
is_callable(array('foo', 'bar'));
function_exists('foo::bar');
my results when doing each operation 10000 times with a simple test class were the following:
is_callable: 0.28671383857727 seconds
function_exists: 0.14569997787476 seconds
(following tests have proved this to be true).
thus you can see, function_exists is twice as fast as is_callable.
breadman
30-Jul-2003 09:17
30-Jul-2003 09:17
Functions within a function are better off as anonymous returns from create_function(), unless you want to be able to call it elsewhere.
However, I have used this in skinning: I use alert_box() to display certain errors, like a faulty SQL query. This simply calls display_alert(), which is defined in my skin scripts. However, alert_box() is sometimes called before I know which skin to load, so it has its own functionality which it uses if function_exists('display_alert') returns false.
dshearin at excite dot com
09-Jul-2003 07:15
09-Jul-2003 07:15
This can be used to conditionally define a user function. In this sense, it can act as a sort of inline include_once().
For example, suppose you have a function A that calls function B. B is only used inside function A and is never called from anywhere else in the script. It's logical (and perfectly legal in PHP) to define B inside of A's definition, like so:
function A($inputArray)
{
if (!function_exists('B'))
{
function B($item)
{
// Do something with $item
// and return result
return $result;
}
}
foreach ($inputArray as $nextItem) $outputArray[] = B($nextItem);
return $outputArray;
}
Without the function_exists test, you would get a fatal error the second time you called A, as PHP would think you were trying to redefine B (not legal in PHP). The placement of the test is also important. Since the if block is executed sequentially, like any other block of code, it must come before any call to the function defined within.
@flop at escapesoft dot net@
07-Dec-2002 01:16
07-Dec-2002 01:16
var_dump(function_exists(create_function('$a','return $a;')));
-> True :))) kweul