Home  >  Article  >  Backend Development  >  How to check if a class method exists in php

How to check if a class method exists in php

青灯夜游
青灯夜游Original
2021-09-26 15:10:202172browse

In PHP, you can use the method_exists() function to check whether a class method exists. This function can check whether a class method exists in the specified object. The syntax is "method_exists($object,$method_name)"; if Returns true if it exists, otherwise returns false.

How to check if a class method exists in php

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

php check whether the class method exists You can use the method_exists() function.

method_exists - Check whether the method of the class exists, check whether the method of the class exists in the specified object.

Syntax:

method_exists($object,$method_name)

Parameters:

  • object: object example or class name.

  • method_name: method name.

Return value: If the method pointed by method_name has been defined in the object class pointed by object, then return true, otherwise return false.

Example:

<?php
$directory = new Directory(&#39;.&#39;);
var_dump(method_exists($directory,&#39;read&#39;));
?>

Output result:

bool(true)

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to check if a class method exists 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