AzeS Posted January 17, 2017 Share Posted January 17, 2017 I am trying to imitate the hashset function of Visual Basic. For this of course, I must declare a variable globally and here it begins to fail already; I have tried it without the interpreter global but also here it does not work at all.You might have better suggestions as I could immit, or even improvements for the code? I look forward to answers and suggestions for improvement. <?php /** * */ global $Hashset = array(); class stimpackZ { public function hash_add($NAME,$DATA) { if ($NAME = 0) { $name_of_hash = count($Hashset) + 1; global $Hashset[$name_of_hash] = $DATA; echo $name_of_hash; return 1; } else { $name_of_hash = $NAME; $Hashset[$name_of_hash] = $DATA; return 1; } } public function hash_create($NAME) { } public function hash_get() { return $Hashset; } public function hash_cls() { global $Hashset = null; global $Hashset = array(); return 1; } public function hash_del($ARG,$DATA) { switch ($ARG) { case 0: $key = array_search($DATA, global $Hashset); unset(global $Hashset[$key]); return 1; break; case 1: unset(global $Hashset[$DATA]); return 1; break; default: return 0; break; } } public function hash_test() { var_dump(global $Hashset); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/302958-hashset-in-php/ Share on other sites More sharing options...
Jacques1 Posted January 17, 2017 Share Posted January 17, 2017 (edited) The code makes no sense. It's generally a bad idea to imitate other languages. Instead, you should specifiy the goal and then implement it with the features of your current language. So what is the goal? A mathematical set which can contain a value exactly once? Then the right implementation is an associative array where the keys are the set elements and the values are arbitrary: <?php $set = []; // store 42 (the value is irrelevant) $set[42] = true; // store 42 again (this has no effect, just like you would expect from a set) $set[42] = true; // check if the set contains certain elements $contains_42 = isset($set[42]); $contains_43 = isset($set[43]); var_dump($contains_42, $contains_43); // delete element unset($set[42]); $contains_42 = isset($set[42]); var_dump($contains_42); Putting this into a class shouldn't be too hard now. I'm sure there are also existing libraries. <?php class Set { private $elements = []; public function add($element) { $this->elements[$element] = true; } public function remove($element) { unset($this->elements[$element]); } public function contains($element) { return isset($this->elements[$element]); } } $set = new Set(); $set->add(42); $set->add(42); var_dump($set->contains(42)); var_dump($set->contains(43)); $set->remove(42); var_dump($set->contains(42)); Edited January 17, 2017 by Jacques1 1 Quote Link to comment https://forums.phpfreaks.com/topic/302958-hashset-in-php/#findComment-1541525 Share on other sites More sharing options...
AzeS Posted January 17, 2017 Author Share Posted January 17, 2017 Thanks for this helpful answer, I will immediately try to imply this and post the result at completion. I leave the thread open if further suggestions are present. Thanks Jacques. Quote Link to comment https://forums.phpfreaks.com/topic/302958-hashset-in-php/#findComment-1541529 Share on other sites More sharing options...
Jacques1 Posted January 17, 2017 Share Posted January 17, 2017 (edited) Note that associative arrays have a couple of quirks which can lead to confusing behavior. Depending on the exact type of the elements (strings? floats? objects?), you may need a different implementation. The above works best for integers and strings. Edited January 17, 2017 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/302958-hashset-in-php/#findComment-1541530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.