PHP judgment function, class, class method exists

Source: Internet
Author: User
Tags bool curl functions mysql

To do PHP development, if you encounter their own can not modify the server related configuration can not know whether some of the server functions are open, the direct use of certain special functions can cause programs to error, such as curl_init such a system function. When the server does not open curl related services, direct use of the Curl series functions will be reported to the call to undefined function curl_init () ... Such a mistake.

So what should be done about this? A lot of things are not just a way, if some methods do not, we can also use another method. Here we need to deal with the problem of determining whether a method exists, if it exists, use the method, or use a different method if the method does not exist.

Here is a collation of how to determine whether a function, class, or method exists in a class:

(1) PHP to determine whether the system function or write their own functions exist

BOOL Function_exists (string $function _name) determines whether a function has been defined, for example:

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

(2) Whether the PHP judge class exists

BOOL Class_exists (String $class _name [, bool $autoload = true]) checks whether a class has been defined, must return true, or FALSE, for example:

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

(3) Whether a method within the PHP judgment class has been defined

the bool Method_exists (mixed $object, string $method _name) checks the existence of the method of the class, for example:

$directory =new directory;
if (!method_exists ($directory, ' read ')) {
	echo ' does not define the Read Method! ';
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.