2017-08-28 99 views
0

我的線程腳本沒有看到線程中的另一個類。這是我的代碼。使用線程中的另一個類

require_once __DIR__.'\vendor\autoload.php'; 
use Symfony\Component\DomCrawler\Crawler; 

$threadCount = 5; 
$list = range(0,100); 
$list = array_chunk($list, $threadCount); 

foreach ($list as $line) { 
    $workers = []; 
    foreach (range(0,count($line)-1) as $i) { 
     $threadName = "Thread #".$i.": "; 
     $workers[$i] = new WorkerThreads($threadName,$line[$i]); 
     $workers[$i]->start(); 
    } 

    foreach (range(0,count($line)-1) as $i) { 
     $workers[$i]->join(); 
    } 
} 

class WorkerThreads extends Thread { 
    private $threadName; 
    private $num; 

    public function __construct($threadName,$num) { 
     $this->threadName = $threadName; 
     $this->num = $num; 
    } 

    public function run() { 
     if ($this->threadName && $this->num) { 
      $result = doThis($this->num); 
      printf('%sResult for number %s' . "\n", $this->threadName, $this->num); 
     } 
    } 
} 

function doThis($num){ 
    $response = '<html><body><p class="message">Hello World!</p></body></html>'; 
    $crawler = new Crawler($response); 
    $data = $crawler->filter('p')->text(); 
    return $data; 
} 

當我運行它,我得到以下錯誤消息

Fatal error: Class 'Symfony\Component\SomeComponent\SomeClass' not found

我將如何讓我的線程看到另一個類?

+0

您是否安裝了symfony中的組件工作的解決方案? – Federkun

+0

@Federkun是的,我有。如果我在線程之外運行'doThis'函數,一切正常。 –

回答

0

隨着@Federkun答案的幫助下,他刪除了,我發現following thread discussing the issue

這裏是

$autoloader = require __DIR__.'\vendor\autoload.php'; 
use Symfony\Component\DomCrawler\Crawler; 

$threadCount = 1; 
$list = range(0,100); 
$list = array_chunk($list, $threadCount); 


foreach ($list as $line) { 
    $workers = []; 
    foreach (range(0,count($line)-1) as $i) { 
     $threadName = "Thread #".$i.": "; 
     $workers[$i] = new WorkerThreads($autoloader,$threadName,$line[$i]); 
     $workers[$i]->start(); 
    } 

    foreach (range(0,count($line)-1) as $i) { 
     $workers[$i]->join(); 
    } 
} 


class WorkerThreads extends Thread { 
    private $threadName; 
    private $num; 

    public function __construct(Composer\Autoload\ClassLoader $loader,$threadName,$num) { 
     $this->threadName = $threadName; 
     $this->num = $num; 
     $this->loader = $loader; 
    } 

    public function run() { 
     $this->loader->register(); 
     if ($this->threadName && $this->num) { 
      $result = doThis($this->num);  
      printf('%sResult for number %s' . "\n", $this->threadName, $this->num); 
     } 
    } 
} 



function doThis($num){ 
    $response = '<html><body><p class="message">Hello World!</p></body></html>'; 
    $crawler = new Crawler($response); 
    $data = $crawler->filter('p')->text(); 
    return $data; 
} 
相關問題