Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
function
web
communitycreator

What is the _exists function group in PHP?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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()

What are these functions?

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.

How do they work?

Below is all there is to know about these three functions.

class_exists()

Syntax

class_exists ($class,$autoload);

Parameter

  • 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.

Return value

It will return true or false.

function_exists()

Syntax

function_exists ($functionName);

Parameter

  • 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.

Return value

It will return a boolean, true or false, after the check is done.

Method_exists()

Syntax

method_exists( $class, $classMethod);

Parameter

  • $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.

Return value

It will return a boolean, true if the method exists in the class and false otherwise, after the check is done.

Code

Let’s see some codes using these functions.

<?php
if (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.

RELATED TAGS

php
function
web
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring