How to create a custom class that does not extend the component class?
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(); } }
error:
PHP Fatal Error β yii\base\ErrorException Class 'common\components\Thread' not found
This class works fine in a clean php application
And Pthread is installed!
source share