Core\ObjectMap::has
Checks if a Core\ObjectMap contains a key. It returns true or false. This replaces isset($storage[$object]) and $storage->contains($object) on an SplObjectStorage.
A key is found with ==. For an object of your own class, has returns true only for the same object you used with set. Another object with equal property values is a different key, so the result for it is false. A Core\Time value such as a Date is compared by its value, so an equal Date is the same key.
has checks the key, not the value. A key whose value is null is still in the map, so has returns true for it. get returns null for that key and for a missing key, so has is the way to tell the two apart. After remove or clear, has returns false.
The examples show a simple check, a key whose value is null, and a common use: doing a piece of work only once for each object.
Parameters
$keyKThe key to look for. It matches a key that is==to it.
Return value
Returns bool. true if the key is in the map, also when its value is null. Otherwise false.
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. |
Examples
Check for a key
<?nvs// Shows how to check if a map contains an object as a key.
final class User{ public string $name;
public function constructor(string $name) { $this->name = $name; }}
User $ada = new User('Ada');User $bob = new User('Bob');
Core\ObjectMap<User, string> $roles = new Core\ObjectMap<User, string>();$roles->set($ada, 'admin');
// Ada is in the map and Bob is not. This prints `true`, then `false`.echo $roles->has($ada) ? 'true' : 'false', "\n";echo $roles->has($bob) ? 'true' : 'false', "\n";
// This object has the same name as Ada, but it is a different object.// This prints `false`.User $copy = new User('Ada');echo $roles->has($copy) ? 'true' : 'false', "\n";truefalsefalseA key whose value is null
<?nvs// Shows that `has` returns `true` for a key whose value is `null`.
final class Upload{ public string $file;
public function constructor(string $file) { $this->file = $file; }}
Upload $photo = new Upload('photo.jpg');Upload $report = new Upload('report.pdf');
// The value is the error message. `null` means the upload had no error.Core\ObjectMap<Upload, ?string> $errors = new Core\ObjectMap<Upload, ?string>();$errors->set($photo, null);
// `get` returns `null` for both uploads. This prints "-" twice.echo $errors->get($photo) ?? '-', "\n";echo $errors->get($report) ?? '-', "\n";
// `has` shows that only the photo was checked. This prints `true`, then `false`.echo $errors->has($photo) ? 'true' : 'false', "\n";echo $errors->has($report) ? 'true' : 'false', "\n";
// After `remove`, the key is gone. This prints `false`.$errors->remove($photo);echo $errors->has($photo) ? 'true' : 'false', "\n";--truefalsefalseSend one email per customer
<?nvs// Shows how to do a piece of work only once for each object, even when it appears many times.
final class Customer{ public string $email;
public function constructor(string $email) { $this->email = $email; }}
final class Order{ public Customer $customer; public int $total;
public function constructor(Customer $customer, int $total) { $this->customer = $customer; $this->total = $total; }}
Customer $ana = new Customer('ana@example.com');Customer $li = new Customer('li@example.com');array<Order> $orders = [new Order($ana, 1200), new Order($li, 350), new Order($ana, 99)];
// The map stores the date of the email sent to each customer.Core\ObjectMap<Customer, string> $sent = new Core\ObjectMap<Customer, string>();
// Ana has two orders, but `has` finds her the second time.// This prints two lines, one for each customer.foreach ($orders as Order $order) { if ($sent->has($order->customer)) { continue; } echo 'email to ', $order->customer->email, "\n"; $sent->set($order->customer, '2026-09-26');}
// This prints 2.echo $sent->count(), "\n";email to ana@example.comemail to li@example.com2