「PHP」method_existsでクラスメソッドが存在するかどうかをチェックする

2021年1月1日

書式
method_exists ( string|object $object , string $method_name ) : bool
method_name で指定したメソッドが 指定した object において定義されている場合に true、そうでない場合に false を返します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$diy = new Directory('.');
var_dump(method_exists($diy,'read'));
?>
<?php $diy = new Directory('.'); var_dump(method_exists($diy,'read')); ?>
<?php
$diy = new Directory('.');
var_dump(method_exists($diy,'read'));      

?>

実行結果
bool(true)

PHP

Posted by arkgame