Skip to content

Core\ObjectSet::add

$objectSet->add(T $value): void$objectSet->add(
    T $value,
): void

Puts a value into a Core\ObjectSet. If the set already has that value, add does nothing, so each value is in the set once. add returns nothing. Use has to check if a value is in the set. This replaces $storage->attach($object) on an SplObjectStorage.

A value is found with ==. For an object of your own class, == is true only for the same object. Two objects with equal property values are two different values, so the set keeps both. A Core\Time value such as a Date is compared by its value, so the set keeps one Date for each day. For a scalar such as an int or a string, equal values are the same value.

The set keeps the value alive while it is in it. A foreach over the set gives the values in the order they were first added.

The examples show adding the same object twice, two equal objects as two members, and a common use: finding the distinct customers in a list of orders.

Parameters

  • $valueTThe value to add, matched with ==.

Return value

Returns void. Nothing. If the set already contains the value, the set does not change. Use has to check whether a value is in the set.

Examples

Add the same object twice

<?nvs
// Shows that `add` puts each value into the set once.
final class Task
{
public string $title;
public function constructor(string $title)
{
$this->title = $title;
}
}
Task $write = new Task('Write report');
Task $send = new Task('Send report');
Core\ObjectSet<Task> $done = new Core\ObjectSet<Task>();
$done->add($write);
// `$write` is already in the set, so this second `add` does nothing.
$done->add($write);
// This prints 1.
echo $done->count(), "\n";
// `has` checks if a value is in the set. This prints "yes", then "no".
echo $done->has($write) ? 'yes' : 'no', "\n";
echo $done->has($send) ? 'yes' : 'no', "\n";
Output
1
yes
no

Equal objects are two members

<?nvs
// Shows that two objects with equal property values are two different members of a set.
final class Point
{
public int $x;
public int $y;
public function constructor(int $x, int $y)
{
$this->x = $x;
$this->y = $y;
}
}
Point $a = new Point(1, 2);
Point $b = new Point(1, 2);
Core\ObjectSet<Point> $points = new Core\ObjectSet<Point>();
$points->add($a);
$points->add($b);
// `$a` and `$b` have the same property values, but they are two objects.
// `$a == $b` is false, so this prints 2.
echo $points->count(), "\n";
// For numbers, equal values are the same value. This prints 2.
Core\ObjectSet<int> $numbers = new Core\ObjectSet<int>();
$numbers->add(7);
$numbers->add(9);
$numbers->add(7);
echo $numbers->count(), "\n";
Output
2
2

Distinct customers in orders

<?nvs
// Shows how to find the distinct customers in a list of orders.
final class Customer
{
public string $name;
public function constructor(string $name)
{
$this->name = $name;
}
}
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');
Customer $li = new Customer('Li');
array<Order> $orders = [
new Order($ana, 30),
new Order($li, 12),
new Order($ana, 45),
];
Core\ObjectSet<Customer> $customers = new Core\ObjectSet<Customer>();
foreach ($orders as Order $order) {
$customers->add($order->customer);
}
// A `foreach` gives the customers in the order they were first added.
// This prints "Ana", then "Li".
foreach ($customers as Customer $customer) {
echo $customer->name, "\n";
}
Output
Ana
Li