0

I've read some examples at github but I didn't get it. If I want use class for storing data in associative array and I want both set data and unset array what approach should I use? I can extend Threaded (is Threaded require to use if I want to share data between threads and why if so?) class and append data to its array propery using foreach, thats fine but how do I unset certain values or even zero the array itself? Sorry for stupid question but I didn't find any clear info about that. Thanks!

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);

And the ouput is:

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

As you can see, $data_array property wont behave as usual array. I'we found that you can make something similar when extending Volatile class, but still what is the way to do it correctly?

2
  • Your question would benefit if you add an example, else it's too broad .. Commented May 13, 2016 at 17:37
  • @dbf, thanks. I've updated the post. Commented May 13, 2016 at 17:53

1 Answer 1

3

A little delayed but for some others who could have the same issue.

You should extend your class from Volatile instead of Threaded, if you intend to change the class attributes dynamically. This is a know "issue" of Threaded class. It is not a real issue, they make immutable attributes for performance reasons. Here is an example.

From php.net:

"The Volatile class is new to pthreads v3. Its introduction is a consequence of the new immutability semantics of Threaded members of Threaded classes. The Volatile class enables for mutability of its Threaded members, and is also used to store PHP arrays in Threaded contexts."

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.