douzhonglong3789 2015-05-16 16:24
浏览 129
已采纳

我如何在php多线程中使用静态方法

i got some problem with php multi thread,here is my code

<?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
";
        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



";
    }
    public function run() {
        echo "sub start
";
        var_dump(testClass::TEST);
        var_dump(testClass::$test1);
        var_dump(testClass::test2());
        var_dump(self::$test3);
        var_dump(self::$test4);
        echo "sub end

";
    }
}
$my = new My();
$my->start();
?>

and the result

#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

in this result , you can see the static String variable can get the value from the sub Thread , but the DateTime object is not

sub Thread call testClass::TEST can get the value, but call testClass::$test1 is not

my PHP Version => 5.6.8 configure '--enable-maintainer-zts'

I am at my wits end, any help at all is greatly appreciated

  • 写回答

1条回答 默认 最新

  • douzhan8652 2015-07-02 20:57
    关注

    From the pthreads introduction:

    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.

    So you have to create your static objects/resources from scratch in each thread.

    Note: This applies to arrays as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵