首次使用Composer运行时报Class 'Timer' not found错误求助
问题原因
你安装的phpunit/php-timer扩展包的Timer类存在命名空间约束,你直接以全局类名Timer调用,自动加载机制无法匹配到对应类文件。
解决方法
你可以选择以下任意一种方式修改你的test.php代码:
- 引入命名空间后调用
在require语句后添加use声明导入Timer类:
<?php require __DIR__ . '/vendor/autoload.php'; // 导入Timer类 use SebastianBergmann\Timer\Timer; Timer::start(); // 你的业务代码 $time = Timer::stop(); var_dump($time); print Timer::secondsToTimeString($time);
- 使用完整限定名直接调用
不需要加use声明,调用时直接写完整的类路径:
<?php require __DIR__ . '/vendor/autoload.php'; \SebastianBergmann\Timer\Timer::start(); // 你的业务代码 $time = \SebastianBergmann\Timer\Timer::stop(); var_dump($time); print \SebastianBergmann\Timer\Timer::secondsToTimeString($time);
额外检查项
如果修改后仍然报错,可以做以下排查:
- 打开
vendor/phpunit/php-timer/src/Timer.php文件,确认顶部的命名空间是否为namespace SebastianBergmann\Timer;,如果是旧版本包命名空间不同,按实际命名空间修改即可 - 确认你执行过
composer require phpunit/php-timer命令,安装过程无报错
内容的提问来源于stack exchange,提问作者43rdworld
相关产品推荐
相关产品推荐

