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-07-05 17:06:122412browse

The way PHP checks whether a class method exists is to use the method_exists method to check, such as [(method_exists($directory,'read'));].

How to check if a class method exists in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

method_exists checks whether the method of the class exists. If the method pointed to by method_name is defined in the object class pointed to by object, then true is returned, otherwise false is returned.

Code example:

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

The above routine will output:

bool(true)

Example 2

<?php
var_dump(method_exists(&#39;Directory&#39;,&#39;read&#39;));
?>

The above routine will output:

bool(true)

Related video tutorial sharing: 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