This article mainly introduces the usage of PHP function class_exists to determine whether a class exists. The example analyzes the application of PHP to determine whether a class exists. For automatically loading classes and the existence judgment before class instantiation It is said that it is of great practical value. Friends in need can refer to
. This article analyzes the usage of the function class_exists in PHP to determine whether a class exists. Share it with everyone 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. Let's look at a few examples.
bool class_exists ( string $class_name [, bool $autoload = true ] )
This function checks whether the given class is defined. This function checks whether or not the given class has been defined.
Returns true if class_name is a defined class, otherwise returns false.
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(); }
The above is the detailed content of Detailed explanation of the usage of function class_exists in PHP to determine whether a class exists. For more information, please follow other related articles on the PHP Chinese website!