Core\ObjectSet::isEmpty
$objectSet->isEmpty(): boolChecks if a Core\ObjectSet has no values. isEmpty returns true for an empty set, and false when the set has at least one value. This replaces count($storage) === 0 on an SplObjectStorage.
A new set is empty. A set is empty again after clear, or after remove takes out its last value. isEmpty gives the same answer as checking that count is 0, and it is shorter to read.
isEmpty does not change the set.
The examples show a set before and after it gets values, a set that is empty again after remove, and a common use: a message that is shown only when there is something to report.
Return value
Returns bool. true for a set with no values, false otherwise.
Examples
A new set is empty
<?nvs// Shows how `isEmpty` checks if a set has any values.
final class Photo{ public string $file;
public function constructor(string $file) { $this->file = $file; }}
Core\ObjectSet<Photo> $selected = new Core\ObjectSet<Photo>();// A new set is empty. This prints "empty".echo $selected->isEmpty() ? 'empty' : 'not empty', "\n";
$selected->add(new Photo('beach.jpg'));// This prints "not empty".echo $selected->isEmpty() ? 'empty' : 'not empty', "\n";emptynot emptyEmpty again after remove
<?nvs// Shows that a set is empty again when its last value is removed.
final class Download{ public string $url;
public function constructor(string $url) { $this->url = $url; }}
Download $first = new Download('https://example.com/a.zip');Download $second = new Download('https://example.com/b.zip');
Core\ObjectSet<Download> $running = new Core\ObjectSet<Download>();$running->add($first);$running->add($second);
$running->remove($first);// One download is still running. This prints "false".echo $running->isEmpty() ? 'true' : 'false', "\n";
$running->remove($second);// This prints "true".echo $running->isEmpty() ? 'true' : 'false', "\n";falsetrueReport only when needed
<?nvs// Shows how to print a report only when some products are low on stock.
final class Product{ public string $name; public int $stock;
public function constructor(string $name, int $stock) { $this->name = $name; $this->stock = $stock; }}
final class StockCheck{ public static function report(array<Product> $products): void { Core\ObjectSet<Product> $low = new Core\ObjectSet<Product>(); foreach ($products as Product $product) { if ($product->stock < 5) { $low->add($product); } } if ($low->isEmpty()) { echo "All products are in stock.\n"; return; } echo "Order more of:\n"; foreach ($low as Product $product) { echo '- ', $product->name, "\n"; } }}
// Every product has 5 or more. This prints "All products are in stock."StockCheck::report([new Product('Pens', 40), new Product('Paper', 12)]);
// Two products have less than 5. This prints a list with "Ink" and "Tape".StockCheck::report([new Product('Ink', 2), new Product('Paper', 12), new Product('Tape', 0)]);All products are in stock.Order more of:- Ink- Tape