在输入数据中,我有文件名,类名和方法名。我需要在没有构造的情况下检查此类中的文件存在(完成)类存在(完成)和方法存在。有可能吗?
答案 0 :(得分:3)
尝试使用反射:
$method = null;
try{
$class = new ReflectionClass('Your_Class');
$method = $class->getMethod('Your_Method');
}
catch(Exception $e){
echo $e->getMessage();
}
if($method != null){
// your code here;
}
答案 1 :(得分:2)
您可以将::class
(需要PHP 5.5+)附加到您的班级名称,并执行以下检查:
<?php
class Test
{
public function someMethod()
{
echo 'Hello, world.';
}
}
var_dump(method_exists(Test::class, 'someMethod'));
答案 2 :(得分:0)
class my_class_name1 {
public function my_method_name1() {
}
}
$class_name = 'my_class_name';
$method_name = 'my_method_name';
echo class_exists($class_name)? ('Class '.$class_name.' exists'):('Class '.$class_name.' does not exist');
echo "\n";
echo class_exists($class_name.'1')? ('Class '.$class_name.'1'.' exists'):('Class '.$class_name.'1'.' does not exist');
echo "\n";
echo method_exists ($class_name.'1',$method_name)? ('Method '.$method_name.' exists in class '.$class_name.'1'):('Method '.$method_name.' does not exists in class '.$class_name.'1');
echo "\n";
echo method_exists ($class_name.'1',$method_name.'1')? ('Method '.$method_name.'1'.' exists in class '.$class_name.'1'):('Method '.$method_name.'1'.'does not exists in class '.$class_name.'1');