Home > Article > Backend Development > How to check if a class method exists in php
The way PHP checks whether a class method exists is to use the method_exists method to check, such as [(method_exists($directory,'read'));].
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('.'); var_dump(method_exists($directory,'read')); ?>
The above routine will output:
bool(true)
Example 2
<?php var_dump(method_exists('Directory','read')); ?>
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!