【问题标题】:Attempted to load class "Thread" from the global namespace. Did you forget a "use" statement?试图从全局命名空间加载类“Thread”。您是否忘记了“使用”声明?
【发布时间】:2016-06-30 22:34:33
【问题描述】:

说明:

创建了一个扩展 Thread 的 php 类 ChildThread。它看起来像这样:

use Thread;

class ChildThread extends Thread
{
    public $data;
    private $anonymous_func;
    public function __construct($anonymous_func)
    {
        $this->anonymous_func = $anonymous_func;
    }

    public function run(){
        $func = $this->anonymous_func;
        $this->data = $func();
    }
}

问题:

我收到以下错误:

试图从全局命名空间加载类“线程”。你是否 忘记“使用”语句?

我显然有一个use 声明,它使用了正确的类:

/**
 * When the start method of a Thread is invoked, the run method code will
 * be executed in separate Thread, asynchronously.<br/>After the run method
 * is executed the Thread will exit immediately, it will be joined with
 * the creating Thread at the approriate time.
 * @link http://www.php.net/manual/en/class.thread.php
 */
class Thread extends Threaded implements Traversable, Countable, ArrayAccess {
  /** class code here **/
}

问题:

为什么说我需要一个“使用”语句,而我已经在使用一个?

【问题讨论】:

    标签: php multithreading symfony


    【解决方案1】:

    您必须安装提供这些类的pthreads 扩展。 PHP Core 中默认不安装它,因为它需要 PHP 的线程安全版本。事实上,PHP 在标准发行版中并没有提供线程安全版本。

    您可以通过获取 PHP 的源代码和compiling it with a special flag 来启用启用了 ZTS 的 PHP (Zend 线程安全)。对于 Windows,也可以直接下载。

    我建议您为 Web 应用程序 (which can't use threads) 使用普通版本的 PHP,为 CLI 应用程序(守护程序等)使用手动编译的 ZTS 版本。

    一旦你有了 PHP 的 ZTS 版本,你就可以通过 PECL 安装 pthreads,或者手动安装它(这比正确编译 PHP 容易;))。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2015-11-07
      • 2019-10-09
      • 1970-01-01
      • 2019-11-18
      • 2020-05-24
      • 1970-01-01
      相关资源
      最近更新 更多