我遇到了php多线程的问题,这是我的代码
<?php
class testClass {
const TEST = 'UTF-8';
public static $test1 = array(
'key' => 'value',
);
public static function test2() {
return "this is static func";
}
}
class My extends Thread {
public static $test3;
public static $test4 = "this is test4 string";
public function __construct() {
echo "main start\n";
self::$test3 = new DateTime();
var_dump(testClass::TEST);
var_dump(testClass::$test1);
var_dump(testClass::test2());
var_dump(self::$test3);
var_dump(self::$test4);
echo "main end\n\n\n\n";
}
public function run() {
echo "sub start\n";
var_dump(testClass::TEST);
var_dump(testClass::$test1);
var_dump(testClass::test2());
var_dump(self::$test3);
var_dump(self::$test4);
echo "sub end\n\n";
}
}
$my = new My();
$my->start();
?>
和结果
#main start
string(5) "UTF-8"
array(1) {
["key"]=>
string(5) "value"
}
string(19) "this is static func"
object(DateTime)#2 (3) {
["date"]=>
string(26) "2015-05-16 23:26:19.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "PRC"
}
string(20) "this is test4 string"
#main end
#sub start
string(5) "UTF-8"
NULL
string(19) "this is static func"
NULL
string(20) "this is test4 string"
#sub end
在这个结果中,你可以看到静态String变量可以从子Thread获取值,但是DateTime对象不是
子线程调用testClass :: TEST可以获取值,但调用testClass :: $ test1不是我的PHP版本=&gt; 5.6.8 配置'--enable-maintainer-zts'
我在我的智慧结束,任何帮助都非常感谢