我如何在 php 多线程中使用静态方法
how can i use static method in php multi thread
我在使用 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 版本 => 5.6.8
配置'--enable-maintainer-zts'
我无计可施,非常感谢任何帮助
Static Members: When a new context is created ( Thread or Worker ), they are generally copied, but resources and objects with internal state are nullified (for safety reasons). This allows them to function as a kind of thread local storage. For example, upon starting the context, a class whose static members include connection information for a database server, and the connection itself, will only have the simple connection information copied, not the connection. Allowing the new context to initiate a connection in the same way as the context that created it, storing the connection in the same place without affecting the original context.
因此您必须在每个线程中从头开始创建静态 objects/resources。
注意:这也适用于 arrays。
我在使用 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 版本 => 5.6.8 配置'--enable-maintainer-zts'
我无计可施,非常感谢任何帮助
Static Members: When a new context is created ( Thread or Worker ), they are generally copied, but resources and objects with internal state are nullified (for safety reasons). This allows them to function as a kind of thread local storage. For example, upon starting the context, a class whose static members include connection information for a database server, and the connection itself, will only have the simple connection information copied, not the connection. Allowing the new context to initiate a connection in the same way as the context that created it, storing the connection in the same place without affecting the original context.
因此您必须在每个线程中从头开始创建静态 objects/resources。
注意:这也适用于 arrays。