php Determines the Existence of Class Function class_exists Usage Analysis

  • 2021-07-26 07:20:40
  • OfStack

In this paper, an example is given to analyze the use of php to judge whether there is a function class_exists in a class. Share it for your reference. The details are as follows:

If we want to judge whether a class can be used, we can first use the class_exists function to judge 1. Let's look at a few examples below.

bool class_exists ( string $class_name [, bool $autoload = true ] )
This function is checked whether the given class is defined. this function checks whether or not the given class has been defined.
Returns true or false if class_name is a defined class.

Examples are as follows:

function __autoload($class)
{
    include($class . '.php');
    // check to see whether the include declared the class
    if (!class_exists($class, false)) {
        trigger_error("unable to load class: $class", e_user_warning);
    }
}
if (class_exists('myclass')) {
    $myclass = new myclass();
}

I hope this article is helpful to everyone's PHP programming.


Related articles: