2016-11-12 34 views

回答

1

$_SERVER是一個超級全局變量,全局變量在創建新線程時不會被pthreads拷貝。

<?php 
class ServerAwareThread extends Thread { 

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

    public function run() { 
     $_SERVER = array_merge(
      $_SERVER ?: [], $this->server); 

     /* show that it's super global */ 
     $this->other(); 
    } 

    public function other() { 
     var_dump($_SERVER); 
    } 
} 

$thread = new ServerAwareThread($_SERVER); 

$thread->start() && $thread->join(); 
?> 

$_SERVER只需通過在新線程的依賴和設置$_SERVER