gpt4 book ai didi

php - 在 yii2 中创建扩展线程的自定义类

转载 作者:可可西里 更新时间:2023-11-01 13:53:22 24 4
gpt4 key购买 nike

如何创建自定义类而不扩展组件类?

类(class):

 namespace common\components;

class AsyncOperation extends Thread {

public function __construct($arg) {
$this->arg = $arg;
}

public function run() {
if ($this->arg) {
$sleep = mt_rand(1, 10);
printf('%s: %s -start -sleeps %d' . "<br />", date("g:i:sa"), $this->arg, $sleep);
sleep($sleep);
printf('%s: %s -finish' . "<br />", date("g:i:sa"), $this->arg);
}
}

}

yii2 Controller :

   public function actionTest() {
// Create a array
$stack = array();

//Iniciate Miltiple Thread
foreach (range("A", "D") as $i) {
$stack[] = new AsyncOperation($i);
}

// Start The Threads
foreach ($stack as $t) {
$t->start();
}
}

错误:

 PHP Fatal Error – yii\base\ErrorException
Class 'common\components\Thread' not found

This class is working perfect in pure php app
And Pthread is installed!

最佳答案

Extends Something 表示在当前命名空间中搜索类 Something\Something 表示在根命名空间中搜索的类。参见 basics of namespaces .

您的 common\components 命名空间中没有类 common\components\Thread。在你的情况下使用 class AsyncOperation extends\Thread {

关于php - 在 yii2 中创建扩展线程的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32734180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com