I am working on a project, in which I am using a deprecated function from the older version. But don’t want my script to stop if used in the older version.
So I am checking if the function exists and if not then create it.
What is the difference between function_exists and is_callable in PHP and which one is better to use?
if (!is_callable('xyz')) {
function xyz() {
// code goes here
}
}
OR
if(!function_exists('xyz')) {
function xyz() {
// code goes here
}
}
The function
is_callableaccepts not only function names, but also other types of callbacks:Foo::methodarray("Foo", "method")array($obj, "method")So
is_callableaccepts anything that you could passcall_user_funcand family, whilefunction_existsonly tells if a certain function exists (not methods, seemethod_existsfor that, nor closures).Put another way,
is_callableis a wrapper forzend_is_callable, which handles variables with the pseudo-type callback, whilefunction_existsonly does a hash table lookup in the functions’ table.