Home > Backend Development > PHP Problem > 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

青灯夜游
Release: 2023-03-13 07:28:01
Original
2149 people have browsed it

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方法!';
}
Copy after login

Description:

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

method_exists(mixed $object, string $method_name): bool
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template