Home  >  Article  >  Backend Development  >  How to determine whether methods and attributes exist in php

How to determine whether methods and attributes exist in php

王林
王林Original
2021-10-19 15:58:392651browse

php method to determine whether methods and attributes exist: [method_exists(mixed $object,string $method_name);property_exists(mixed $class,string $property);].

How to determine whether methods and attributes exist in php

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

php determines whether a method in the class exists:

bool method_exists (mixed $object, string $method_name) Check whether the method of the class exists, for example:

$directory=new Directory;
if(!method_exists($directory,'read')){
    echo '未定义read方法!';
}

PHP determines whether a property in the class has been defined:

bool property_exists(mixed $class, string $property) Check whether the property of the class exists, for example:

$directory=new Directory;
if(!property_exists($directory,'li')){
    echo '未定义li属性!';
}

Recommended learning: phptraining

The above is the detailed content of How to determine whether methods and attributes exist 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