关于PHP多线程pthreads调用静态类的疑问

新手上路,请多包涵
<?php
class b extends Thread {
    public $func = null;

    public function run() {
        $c = new c();

        var_dump($c->listA);
        var_dump(c::$listB);
        var_dump(c::$hello);
    }
}

class c {
    public $listA = ['a', 'b', 'c'];
    public static $listB = ['a', 'b', 'c'];
    public static $hello = 'hello world!';
}

$b = new b();
$b->start();

输出值为:


array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}
NULL
string(12) "hello world!"

WTF?
大神们,$listB 的数组去哪了?

阅读 2.9k
1 个回答

我使用你的程序,得到的是正确的结果:

root@f4c57ecbbc9f:/data/php/pthreads# cat test.php
<?php
class b extends Thread {
    public $func = null;

    public function run() {
        $c = new c();

        var_dump($c->listA);
        var_dump(c::$listB);
        var_dump(c::$hello);
    }
}

class c {
    public $listA = ['a', 'b', 'c'];
    public static $listB = ['a', 'b', 'c'];
    public static $hello = 'hello world!';
}
$b = new b();
$b->start();

root@f4c57ecbbc9f:/data/php/pthreads# php test.php 
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}
string(12) "hello world!"

环境如下:

  • PHP
root@f4c57ecbbc9f:/data/php/pthreads# php -v
PHP 7.2.0beta1 (cli) (built: Jul 24 2017 19:04:41) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0-dev, Copyright (c) 1998-2017 Zend Technologies
  • pthreads的扩展直接从github里clone下来的
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题