Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php pthreads数组问题_Php_Arrays_Oop_Pthreads - Fatal编程技术网

php pthreads数组问题

php pthreads数组问题,php,arrays,oop,pthreads,Php,Arrays,Oop,Pthreads,我在github上读过一些例子,但我没有理解。如果我想使用类在关联数组中存储数据,并且我想同时使用集合数据和未设置数组,我应该使用什么方法?我可以使用foreach扩展Threaded(如果我想在线程之间共享数据,Threaded是否需要使用,为什么要使用?)类并将数据附加到其数组属性,这很好,但如何取消设置某些值,甚至将数组本身置零?很抱歉问了个愚蠢的问题,但我没有找到任何关于这个问题的清晰信息。谢谢 class Test extends Threaded{ private $dat

我在github上读过一些例子,但我没有理解。如果我想使用类在关联数组中存储数据,并且我想同时使用集合数据和未设置数组,我应该使用什么方法?我可以使用foreach扩展Threaded(如果我想在线程之间共享数据,Threaded是否需要使用,为什么要使用?)类并将数据附加到其数组属性,这很好,但如何取消设置某些值,甚至将数组本身置零?很抱歉问了个愚蠢的问题,但我没有找到任何关于这个问题的清晰信息。谢谢

class Test extends Threaded{

    private $data_array = array();

    public function FillArray($add){
        foreach($add as $a){
            $this->data_array[] = $a;
        }


    }


    public function RemoveItem($item){
        if( ($key = array_search($item, (array)$this->data_array)) !== false )  
        unset($this->data_array[$key]);
    }

    public function Clear(){        
        $this->data_array = array();
    }

    public function FilterUnique(){
        $this->data_array = array_unique((array)$this->data_array);
    }


}


$t = new Test();
$arr = [ 'test string', 'test2', 'element3', 'test string', '1234element'];
$t->FillArray($arr);
var_dump($t);
$t->RemoveItem('test2');
var_dump($t);
$t->FilterUnique();
$t->Clear();
var_dump($t);
输出为:

C:\xampp\htdocs\w\functions\t1.php:36:
class Test#1 (1) {
  public $data_array =>
  class Volatile#2 (5) {
    public ${0} =>
    string(11) "test string"
    public ${1} =>
    string(5) "test2"
    public ${2} =>
    string(8) "element3"
    public ${3} =>
    string(11) "test string"
    public ${4} =>
    string(11) "1234element"
  }
}
C:\xampp\htdocs\w\functions\t1.php:38:
class Test#1 (1) {
  public $data_array =>
  class Volatile#2 (4) {
    public ${0} =>
    string(11) "test string"
    public ${2} =>
    string(8) "element3"
    public ${3} =>
    string(11) "test string"
    public ${4} =>
    string(11) "1234element"
  }
}

Fatal error: Uncaught RuntimeException: Threaded members previously set to Threa
ded objects are immutable, cannot overwrite data_array in C:\xampp\htdocs\w\func
tions\t1.php on line 26

RuntimeException: Threaded members previously set to Threaded objects are immuta
ble, cannot overwrite data_array in C:\xampp\htdocs\w\functions\t1.php on line 2
6

Call Stack:
    0.0020     358208   1. {main}() C:\xampp\htdocs\w\functions\t1.php:0
    0.0030     359904   2. Test->FilterUnique() C:\xampp\htdocs\w\functions\t1.p
hp:39

正如您所看到的,$data\u数组属性不会像通常的数组那样工作。我想我们发现,在扩展Volatile类时,您可以做一些类似的事情,但正确的方法是什么?

有点延迟,但对于其他可能有相同问题的人来说

如果要动态更改类属性,应该从Volatile而不是线程扩展类。 这是线程类的一个已知“问题”。这不是一个真正的问题,因为性能原因,它们会生成不变的属性。

:

“Volatile类是pthreadsv3中的新类。它的介绍是 的线程成员的新不变性语义的结果 线程类。Volatile类支持其 线程化成员,还用于将PHP数组存储在线程化 上下文。”


有点延迟,但对于其他可能有同样问题的人来说

如果要动态更改类属性,应该从Volatile而不是线程扩展类。 这是线程类的一个已知“问题”。这不是一个真正的问题,因为性能原因,它们会生成不变的属性。

:

“Volatile类是pthreadsv3中的新类。它的介绍是 的线程成员的新不变性语义的结果 线程类。Volatile类支持其 线程化成员,还用于将PHP数组存储在线程化 上下文。”


如果您添加一个示例,您的问题将受益匪浅,否则它太宽泛了。@dbf,谢谢。我已经更新了帖子。如果你添加一个例子,你的问题会有所帮助,否则太宽泛了。@dbf,谢谢。我已经更新了帖子。