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 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置
  • ¥15 excel 日常使用中出现问题