Core\ObjectSet::has
Checks if a value is in a Core\ObjectSet. has returns true when the set has the value, and false when it does not. This replaces $storage->contains($object) on an SplObjectStorage.
A value is found with ==. For an object of your own class, has returns true only for the same object that was added. An object with equal property values is a different object, so the result is false. For a scalar such as an int or a string, an equal value is the same value. A Core\Time value such as a Date is compared by its value too.
has does not change the set. It is fast, even for a very large set, because it does not look at every value.
The examples show has before and after add and remove, an equal object that is not the same object, and a common use: skipping a job that was already done. The last one checks a set of booked days with a Date read from text.
Parameters
$valueTThe value to look for, matched with==.
Return value
Returns bool. true when the value is in the set, false otherwise.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\ObjectMap and Core\ObjectSet are faster. A lookup with a string, int or object key costs about the same as a lookup in an array. A foreach over either does not copy it. |
0.0.6 | Core\ObjectMap and Core\ObjectSet find a key with ==. An equal Core\Time value is the same key, so Date::at(2026, 3, 9) finds the entry of another Date for that day. Objects of your own classes are still found only by identity. |
Examples
Check before and after add
<?nvs// Shows how `has` checks if a value is in a set, before and after you change the set.
final class Room{ public string $name;
public function constructor(string $name) { $this->name = $name; }}
Room $kitchen = new Room('Kitchen');
Core\ObjectSet<Room> $cleaned = new Core\ObjectSet<Room>();// The set is empty. This prints "no".echo $cleaned->has($kitchen) ? 'yes' : 'no', "\n";
$cleaned->add($kitchen);// This prints "yes".echo $cleaned->has($kitchen) ? 'yes' : 'no', "\n";
$cleaned->remove($kitchen);// This prints "no".echo $cleaned->has($kitchen) ? 'yes' : 'no', "\n";noyesnoEqual is not the same object
<?nvs// Shows that `has` finds an object of your own class only when it is the same object.
final class Point{ public int $x; public int $y;
public function constructor(int $x, int $y) { $this->x = $x; $this->y = $y; }}
Point $start = new Point(0, 0);Point $copy = new Point(0, 0);
Core\ObjectSet<Point> $visited = new Core\ObjectSet<Point>();$visited->add($start);
// `$start` is in the set. This prints "true".echo $visited->has($start) ? 'true' : 'false', "\n";
// `$copy` has the same property values, but it is a different object. This prints "false".echo $visited->has($copy) ? 'true' : 'false', "\n";
// Strings are matched by value. This prints "true".Core\ObjectSet<string> $words = new Core\ObjectSet<string>();$words->add('apple');echo $words->has('app' . 'le') ? 'true' : 'false', "\n";truefalsetrueSkip jobs already done
<?nvs// Shows how to skip a job that already ran, when the same job is in a queue more than once.
final class Job{ public string $name;
public function constructor(string $name) { $this->name = $name; }}
Job $invoice = new Job('send invoice');Job $report = new Job('build report');Job $backup = new Job('nightly backup');
// The same job object is in the queue more than once.array<Job> $queue = [$invoice, $report, $invoice, $backup, $report];
Core\ObjectSet<Job> $done = new Core\ObjectSet<Job>();// This prints "run" for three jobs and "skip" for the two repeats.foreach ($queue as Job $job) { if ($done->has($job)) { echo 'skip ', $job->name, "\n"; continue; } echo 'run ', $job->name, "\n"; $done->add($job);}run send invoicerun build reportskip send invoicerun nightly backupskip build reportA date built again is in the set
<?nvs// Shows that a set of `Core\Time\Date` values finds a date that was built again.
Core\ObjectSet<Core\Time\Date> $booked = new Core\ObjectSet<Core\Time\Date>();$booked->add(Core\Time\Date::at(2026, 7, 14));$booked->add(Core\Time\Date::at(2026, 7, 15));
// A `Date` is compared by its day, so the same day is not added twice. This prints 2.$booked->add(Core\Time\Date::fromIso('2026-07-14'));echo $booked->count(), "\n";
// This date is a new object, read from text. `has` finds it. This prints "booked".var $wanted = Core\Time\Date::fromIso('2026-07-15');echo $booked->has($wanted) ? 'booked' : 'free', "\n";
// No value in the set has this day. This prints "free".echo $booked->has(Core\Time\Date::at(2026, 7, 16)) ? 'booked' : 'free', "\n";2bookedfree