【问题标题】:classes autoload doesn't work when imported from another file从另一个文件导入时,类自动加载不起作用
【发布时间】:2019-05-22 08:35:54
【问题描述】:

当我从另一个文件导入时,我的 autoload.php 没有加载所需的类:

这是我自动加载的类:

main.php:

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;
require '/library/vendor/autoload.php';

class Main {
    __construct() {
    $this->socketIOClient = new Client(new Version2X('someWebsite'));
    }
}

以下脚本有效:

require 'main.php';
$main = new Main();

但是这个脚本没有:

require '/library/vendor/autoload.php';
require 'main.php';

class NotWorking extends Thread {
    __construct() {
        $this->main = new Main();
    }
}
$nowWorking = new NotWorking();

Output:

Fatal error: Uncaught Error: Class 'ElephantIO\Client' not found in main.php

为什么会这样,再次正确自动加载的方法是什么?

提前感谢您的帮助!

编辑:

问题是我使用的 pthreads 和扩展线程的类在需要 autoload.php 时表现不正常。

【问题讨论】:

  • 我看不到第一个脚本是如何工作的。您正在尝试在包含自动加载之前使用一个类。应该是第一个脚本抛出错误

标签: php namespaces composer-php autoload


【解决方案1】:

把需求放在第一位。

require '/library/vendor/autoload.php';

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;

【讨论】:

  • 问题是我使用的 pthreads 和扩展线程的类在需要 autoload.php 时表现不正常。
猜你喜欢
  • 2019-06-27
  • 2013-01-21
  • 1970-01-01
  • 2021-02-10
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
相关资源
最近更新 更多