There is no function known as “_exist” in PHP. Instead, there is a set of functions in PHP with the suffix _exist
. They all do an existence check of the indicated prefix.
This set of functions includes:
class_exists()
function_exists()
method_exists()
These functions are inbuilt in PHP. They are mainly boolean operators in terms of how they only evaluate to a true
or false
value.
The class_exists()
function will check if a class has been defined or not. In the same vein, the function_exists()
function will check if a function is part of the function haul in the code. This function haul/list includes both the built-in function and those defined by users.
Lastly, among this special list, the method_exists()
function will check if a method exists in the class or object instance, as indicated.
Below is all there is to know about these three functions.
class_exists()
class_exists ($class,$autoload);
Class parameter: This indicates the name of the class to be checked for. This check is case sensitive.
Autoload parameter: This will indicate if the _autoload()
should be called automatically or not. This value can either be true
or false
.
It will return true
or false
.
function_exists()
function_exists ($functionName);
functionName
: This is the only parameter required by the function_exists()
function. It is a string value of the name of the function being checked.It will return a boolean, true
or false
, after the check is done.
Method_exists()
method_exists( $class, $classMethod);
$class parameter
: This is the class which will be checked for the existence of the indicated method.
$classMethod parameter
:
This is the name of the method.
It will return a boolean, true
if the method exists in the class and false
otherwise, after the check is done.
Let’s see some codes using these functions.
<?phpif (function_exists('imap_open')) {echo "IMAP functions are available.<br />\n";} else {echo "IMAP functions are not available.<br />\n";}$folder = new Directory('.');var_dump(method_exists($folder,'read'));if (class_exists('newClass')) {echo "true";$myclass = new newClass();}else{echo "false";}?>
There are other function of the _exists
type, such as property_exists
, traits_exists
, ennum_exist
, and others. They basically check for the existence of the prefix of their name.