Home  >  Article  >  Backend Development  >  How to determine whether functions, classes, and class methods exist in PHP

How to determine whether functions, classes, and class methods exist in PHP

怪我咯
怪我咯Original
2017-06-28 13:35:299921browse

During PHP development, if you encounter a situation where you cannot modify the relevant configuration of the server or know whether certain functions of the server are enabled, directly using some special functions will cause the program to report an error, such as curl_init is a system function. When the server does not enable curl-related services, directly using the curl series functions will report an error such as Call to undefined function curl_init()....

So what should we do if this happens? There is not just one way to do many things. If some methods don't work, we can also use another method. Here we need to involve the problem of judging whether a certain method exists. If the method exists, use this method. If the method does not exist, use another method.

Here is a summary of how to determine whether a function, class, and method in a class exists:

(1) PHP determines whether a system function or a function written by yourself exists

bool function_exists ( string $function_name ) Determines whether the function has been defined, for example:

if(function_exists('curl_init')){
	curl_init();
}else{
	echo 'not function curl_init';
}

(2) PHP determines whether the class exists

bool class_exists ( string $class_name [, bool $autoload = true ] ) Checks whether a class has been defined. It must return true, otherwise it returns false, for example:

if(class_exists('MySQL')){
    $myclass=new MySQL();
}

(3) PHP determines whether a class is defined Whether a method has been defined

bool method_exists (mixed $object, string $method_name) Check whether the method of the class exists, for example:

$directory=new Directory;
if(!method_exists($directory,'read')){
	echo '未定义read方法!';
}

The above is the detailed content of How to determine whether functions, classes, and class methods exist in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn