Skip to content

Core\ObjectSet::union

$objectSet->union(Core\ObjectSet $other): Core\ObjectSet$objectSet->union(
    Core\ObjectSet $other,
): Core\ObjectSet

Builds a new Core\ObjectSet with every value that is in this set, in $other, or in both. A value that is in both sets is in the result only once. This replaces a loop that copies two SplObjectStorage objects into a third one.

A value is found with ==. For an object of your own class, two objects with equal property values are two different values, so the result has both of them. Two Date values for the same day are one value.

The result is a new set. union does not change this set or $other. The values of this set come first, in their order. Then the values of $other that are not in this set follow, in their order.

The examples show two sets combined into one, a value that is in both sets, and a common use: one list of people to send a message to, where nobody gets the message twice.

Parameters

  • $otherCore\ObjectSetThe set to combine with.

Return value

Returns Core\ObjectSet. A new Core\ObjectSet<T>. The values of this set come first, in their order. Then the values of $other that are not in this set follow. Neither set is changed.

Examples

Combine two sets

<?nvs
// Shows how to combine two sets into one new set.
final class Tag
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Tag $red = new Tag('red');
Tag $blue = new Tag('blue');
Tag $green = new Tag('green');
Core\ObjectSet<Tag> $warm = new Core\ObjectSet<Tag>();
$warm->add($red);
Core\ObjectSet<Tag> $cool = new Core\ObjectSet<Tag>();
$cool->add($blue);
$cool->add($green);
// `union` returns a new set. The values of `$warm` come first.
Core\ObjectSet<Tag> $all = $warm->union($cool);
// This prints red, blue and green, one on each line.
foreach ($all as Tag $tag) {
echo $tag->name, "\n";
}
// `$warm` and `$cool` do not change. This prints "1 2".
echo $warm->count(), ' ', $cool->count(), "\n";
Output
red
blue
green
1 2

A value in both sets

<?nvs
// Shows that a value in both sets is in the result only once.
final class Tag
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Tag $sale = new Tag('sale');
Core\ObjectSet<Tag> $first = new Core\ObjectSet<Tag>();
$first->add($sale);
$first->add(new Tag('new'));
Core\ObjectSet<Tag> $second = new Core\ObjectSet<Tag>();
$second->add($sale);
$second->add(new Tag('new'));
// `$sale` is the same object in both sets, so the result has it once.
// The two "new" tags are two different objects, so the result has both.
Core\ObjectSet<Tag> $both = $first->union($second);
// This prints 3.
echo $both->count(), "\n";
// This prints "sale", "new" and "new", one on each line.
foreach ($both as Tag $tag) {
echo $tag->name, "\n";
}
Output
3
sale
new
new

People to notify

<?nvs
// Shows how to build one list of people to send a message to, so that nobody gets it twice.
final class Person
{
public string $email;
public function constructor(string $email)
{
$this->email = $email;
}
}
final class Ticket
{
public Core\ObjectSet<Person> $watchers;
public Core\ObjectSet<Person> $team;
public function constructor()
{
$this->watchers = new Core\ObjectSet<Person>();
$this->team = new Core\ObjectSet<Person>();
}
// Everybody who watches the ticket or works in the team.
public function recipients(): Core\ObjectSet<Person>
{
return $this->watchers->union($this->team);
}
}
Person $ana = new Person('ana@example.com');
Person $li = new Person('li@example.com');
Person $sam = new Person('sam@example.com');
Ticket $ticket = new Ticket();
$ticket->watchers->add($ana);
$ticket->watchers->add($li);
$ticket->team->add($li);
$ticket->team->add($sam);
// Li watches the ticket and is also in the team. Li gets one message.
// This prints three lines: Ana, Li and Sam.
foreach ($ticket->recipients() as Person $person) {
echo 'Send to ', $person->email, "\n";
}
Output
Send to ana@example.com
Send to li@example.com
Send to sam@example.com