Skip to content

Core\ObjectSet::difference

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

Builds a new Core\ObjectSet with the values that are in this set and not in $other. This replaces a loop over one SplObjectStorage that skips each object another one contains. Core\Arr::difference does the same for arrays.

The order of the two sets matters. $a->difference($b) gives the values that only $a has. $b->difference($a) gives the values that only $b has.

A value matches when it is == to a value in the other set. For an object of your own class, that is only the same object. The result is a new set, and difference does not change this set or $other. The values keep the order they have in this set. If $other has every value of this set, the result is an empty set.

The examples show the values that only one set has, and why the order of the two sets matters. The last one shows a common use: finding the guests who have not answered an invitation yet.

Parameters

  • $otherCore\ObjectSetThe set whose values are left out.

Return value

Returns Core\ObjectSet. A new Core\ObjectSet<T>, in the order of this set. It is empty when $other has every value of this set. Neither set is changed.

Changelog

VersionChange
0.0.6Core\ObjectSet->intersection() and difference() return a new set. The old names were intersect and diff.

Examples

Values only in this set

<?nvs
// Shows how to find the values that are in one set and not in another.
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> $all = new Core\ObjectSet<Tag>();
$all->add($red);
$all->add($blue);
$all->add($green);
Core\ObjectSet<Tag> $used = new Core\ObjectSet<Tag>();
$used->add($blue);
// `difference` returns a new set without the values of `$used`.
Core\ObjectSet<Tag> $unused = $all->difference($used);
// This prints the values of the new set, red and green, one on each line.
foreach ($unused as Tag $tag) {
echo $tag->name, "\n";
}
// `$all` and `$used` do not change. This prints "3 1".
echo $all->count(), ' ', $used->count(), "\n";
Output
red
green
3 1

The order matters

<?nvs
// Shows that `$a->difference($b)` and `$b->difference($a)` give different results.
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> $before = new Core\ObjectSet<Tag>();
$before->add($red);
$before->add($blue);
Core\ObjectSet<Tag> $after = new Core\ObjectSet<Tag>();
$after->add($blue);
$after->add($green);
// The tags that were removed. This prints "Removed red".
foreach ($before->difference($after) as Tag $tag) {
echo 'Removed ', $tag->name, "\n";
}
// The tags that were added. This prints "Added green".
foreach ($after->difference($before) as Tag $tag) {
echo 'Added ', $tag->name, "\n";
}
Output
Removed red
Added green

Guests who have not answered

<?nvs
// Shows how to find the guests who have not answered an invitation, to send them a reminder.
final class Guest
{
public string $email;
public function constructor(string $email)
{
$this->email = $email;
}
}
final class Invitation
{
public Core\ObjectSet<Guest> $invited;
public Core\ObjectSet<Guest> $answered;
public function constructor()
{
$this->invited = new Core\ObjectSet<Guest>();
$this->answered = new Core\ObjectSet<Guest>();
}
public function invite(Guest $guest): void
{
$this->invited->add($guest);
}
public function answer(Guest $guest): void
{
$this->answered->add($guest);
}
// The guests who were invited and have not answered yet.
public function waiting(): Core\ObjectSet<Guest>
{
return $this->invited->difference($this->answered);
}
}
Guest $ana = new Guest('ana@example.com');
Guest $li = new Guest('li@example.com');
Guest $sam = new Guest('sam@example.com');
Invitation $party = new Invitation();
$party->invite($ana);
$party->invite($li);
$party->invite($sam);
$party->answer($li);
// This prints two lines, for Ana and Sam.
foreach ($party->waiting() as Guest $guest) {
echo 'Remind ', $guest->email, "\n";
}
$party->answer($ana);
$party->answer($sam);
// Everybody has answered, so the result is empty. This prints 0.
echo $party->waiting()->count(), "\n";
Output
Remind ana@example.com
Remind sam@example.com
0