Home  >  Article  >  Backend Development  >  How to determine whether a specified method is defined in a class in PHP

How to determine whether a specified method is defined in a class in PHP

青灯夜游
青灯夜游Original
2021-09-29 19:42:042088browse

You can use the method_exists() function in php to determine whether the specified method is defined in the class. This function can check whether the specified method of the class exists. The syntax is "method_exists($object,$method_name)"; if it is defined It returns true, otherwise it returns false.

How to determine whether a specified method is defined in a class in PHP

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use method_exists () function to determine whether the specified method is defined in the class.

Example:

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

Description:

The method_exists() function can check whether the specified method of the class exists. Syntax:

method_exists(mixed $object, string $method_name): bool

This function checks whether the class method exists in the specified object.

  • object: object example or class name.

  • method_name: method name.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine whether a specified method is defined in a class 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