2016-11-12 56 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