https://en.wikipedia.org/wiki/Set_(mathematics)
Sets can contain numbers, strings, arrays, objects, and other sets.
Implementation:
For performance reasons, PHP arrays are used as a hash for quick access
via hash keys.
The hash keys are as follows:
- Numbers and strings: value itself
- Sets: Set as a string.
- Arrays: Array(array_serialization)
- Objects: Object\Name(object_hash)
- Resource: Resource(Resource id: #)
- Null: ''
The values of the associative array (hash) are the actual values or
objects themselves. If the set is iterated in a foreach loop you will
get back the original value, set, array, or object.
An object cannot be in the set multiple times. For a regular value, like
a number or string, this is straight forward. For arrays and objects, the
behavior is based on whether they are the same thing. What that means depends
on whether it is an array or object.
Example (arrays):
$array1 = [1, 2, 3];
$array2 = [1, 2, 3];
$set = new Set([$array1, $array2]);
The set will have only one element, because the arrays are equal.
$array2 === $array2 evaluates to true.
Example (different objects):
$object1 = new \StdClass();
$object2 = new \StdClass();
$set = new Set([$object1, $object2]);
The set will have two elements, because they are different objects.
$object1 === $object2 evaluates to false.
Example (same objects):
$object1 = new \StdClass();
$object2 = $object1;
$set = new Set([$object1, $object2]);
The set will have only one element, because the objects are the same.
$object1 === $object2 evaluates to true.
Example (Sets, a special case of object)
$set1 = new Set([1, 2, 3]);
$set2 = new Set([1, 2, 3]);
$set3 = new Set([$set1, $set2]);
Set3 will have only one element, because sets 1 and 2 are the same. Sets are
not based on whether the object is the same, but whether the content of
the set are the same. Sets and arrays act similarly.
When storing a Set object as a member of a set, its key will be a string
that uses mathematical set notation with the addtion of the word 'Set'.
For example: Set{1, 2, 3}
The one edge case of this, is that the Set object {1, 2, 3} and the string
'Set{1, 2, 3}' would appear identical in the case of adding one when the other
already is a member of the set. When accessing the actual set member, you
will always get back the original one added, whether it was a Set object or
a string.