PHP ReflectionClass方法不存在

时间:2017-02-20 18:11:22

标签: php reflection laravel-5.4

我试图通过这样的反射实例化一个类:

$class = new ReflectionClass('App\\Widgets\\TestWidget');

但是这总是抛出一条带有这条消息的ReflectionException:

[2017-02-20 19:05:25] local.ERROR: ReflectionException while trying to reflect C:\Users\Asier\PhpstormProjects\web-cms\app\Widgets\TestWidget\TestWidget.php
Class App\Widgets\TestWidget implements Widget {

/**
 * It is called when the widget is used in a template to render it.
 * @return string
 */
public function render() {
    return "Hello world from TestWidget";
}

/**
 * Returns the widget's name
 * @return string
 */
public static function getWidgetName() {
    return "TestWidget";
}

/**
 * Returns the widget's info
 * @return string|array
 */
public static function getWidgetInfo() {
    return "";
}

/**
 * It is called when the widget is uploaded to make the proper setup for it if needed
 * @return void
 */
public static function setup() {

}

public function dummy() does not exist

这是班级:

<?php
namespace App\Widgets;


class TestWidget implements Widget {

/**
 * It is called when the widget is used in a template to render it.
 * @return string
 */
public function render() {
    return "Hello world from TestWidget";
}

/**
 * Returns the widget's name
 * @return string
 */
public static function getWidgetName() {
    return "TestWidget";
}

/**
 * Returns the widget's info
 * @return string|array
 */
public static function getWidgetInfo() {
    return "";
}

/**
 * It is called when the widget is uploaded to make the proper setup for it if needed
 * @return void
 */
public static function setup() {

}

public function dummy() {}

}

如果我删除了最后一个方法(虚拟),那么它表示相同的&#34;不存在&#34;使用班上的最后一个方法。

有什么想法吗?

P.D。:使用Laravel 5.4。

编辑:好的伙计们。问题解决了。我累了。我有一个带有正则表达式的方法,该表达式与脚本匹配并获取其类名,结果突然它停止按预期工作,并且该组几乎将整个脚本作为字符串而不是正确的类名返回。

现在修复如下:

/**
 * Returns the class name of the given php script
 * @param $script real path to the script
 * @return mixed
 */
public function className($script) {
    $match = preg_match('/class ([A-Za-z0-9_]{1,}) +.* {/ism', file_get_contents($script), $className);
    return $className[1];
}

感谢所有人试一试。对不起。

0 个答案:

没有答案