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?