How to check if a class method exists in php

王林
Release: 2023-03-11 09:06:01
Original
2460 people have browsed it

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;));
?>
Copy after login

The above routine will output:

bool(true)
Copy after login
Copy after login

Example 2

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

The above routine will output:

bool(true)
Copy after login
Copy after login

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!

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