Skip to content

Core\ObjectMap::isEmpty

$objectMap->isEmpty(): bool

Checks if a Core\ObjectMap has no entries. It returns true when the map is empty and false when it has at least one key. $map->isEmpty() gives the same answer as $map->count() == 0, and it is shorter to read.

isEmpty counts keys, not values. A key whose value is null is still an entry, so a map with only that key is not empty. The map is empty again after remove takes out its last key, or after clear.

The examples show a new map, a map with a null value, and a common use: saving changed records at the end of a request, only when there is something to save.

Return value

Returns bool. true for a map with no keys, false otherwise.

Examples

A new map is empty

<?nvs
// Shows how to check if a map has any entries.
final class Room
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
Core\ObjectMap<Room, int> $guests = new Core\ObjectMap<Room, int>();
// A new map has no entries. This prints `true`.
echo $guests->isEmpty() ? 'true' : 'false', "\n";
// After one `set`, the map is not empty. This prints `false`.
$guests->set(new Room('Blue'), 2);
echo $guests->isEmpty() ? 'true' : 'false', "\n";
Output
true
false

A null value is an entry

<?nvs
// Shows that a key whose value is `null` is an entry, and when the map becomes empty again.
final class Task
{
public string $title;
public function constructor(string $title)
{
$this->title = $title;
}
}
Task $backup = new Task('backup');
Task $report = new Task('report');
// The value is the name of the person working on the task. `null` means nobody yet.
Core\ObjectMap<Task, ?string> $owner = new Core\ObjectMap<Task, ?string>();
$owner->set($backup, null);
// The map has one key, even though its value is `null`. This prints `false`.
echo $owner->isEmpty() ? 'true' : 'false', "\n";
// `remove` takes out the last key. This prints `true`.
$owner->remove($backup);
echo $owner->isEmpty() ? 'true' : 'false', "\n";
// `clear` also empties the map. This prints `false`, then `true`.
$owner->set($backup, 'Ana');
$owner->set($report, 'Li');
echo $owner->isEmpty() ? 'true' : 'false', "\n";
$owner->clear();
echo $owner->isEmpty() ? 'true' : 'false', "\n";
Output
false
true
false
true

Save only when something changed

<?nvs
// Shows how to save changed records at the end of a request, only when something changed.
final class Product
{
public string $name;
public int $price;
public function constructor(string $name, int $price)
{
$this->name = $name;
$this->price = $price;
}
}
final class Changes
{
// The map stores the old price of each product that changed.
public Core\ObjectMap<Product, int> $old;
public function constructor()
{
$this->old = new Core\ObjectMap<Product, int>();
}
public function setPrice(Product $product, int $price): void
{
if (!$this->old->has($product)) {
$this->old->set($product, $product->price);
}
$product->price = $price;
}
public function save(): void
{
// `isEmpty` is `true` when no product changed, so there is nothing to write.
if ($this->old->isEmpty()) {
echo "nothing to save\n";
return;
}
// This loop gives each changed product as the key, and its old price as the value.
foreach ($this->old as Product $product => int $oldPrice) {
echo 'save ', $product->name, ': ', $oldPrice, ' -> ', $product->price, "\n";
}
$this->old->clear();
}
}
Product $lamp = new Product('lamp', 4500);
Changes $changes = new Changes();
// No price changed yet. This prints "nothing to save".
$changes->save();
// The price changes twice. The map keeps the first old price, 4500.
$changes->setPrice($lamp, 4200);
$changes->setPrice($lamp, 3900);
// This prints "save lamp: 4500 -> 3900".
$changes->save();
Output
nothing to save
save lamp: 4500 -> 3900