<?php
class Foo
{
const CHANGED = 1;
const IS_NEW = 2;
public $value = 1;
}
$o1 = new Foo();
$o2 = new Foo();
$o3 = new Foo();
// initial storage states, all unchanged
$storage = new SplObjectStorage();
$storage->attach($o1);
$storage->attach($o2);
$storage->attach($o3);
// change happens to one of the Foo instances
$o2->value = 5;
if (true === $storage->contains($o2)) {
$storage[$o2] = Foo::CHANGED;
}
// A new Foo instance appears
$o4 = new Foo();
$o4->value = 8;
if (false === $storage->contains($o4)) {
$storage->attach($o4, Foo::IS_NEW);
}
// At the end, handle all the states of the objects
foreach ($storage as $obj) {
switch ($storage[$obj]) {
case Foo::CHANGED:
echo 'Update Foo, value: ' . $obj->value;
break;
case Foo::IS_NEW:
echo 'Save new Foo, value: ' . $obj->value;
break;
default:
echo 'Unchanged: ' . $obj->value;
}
echo PHP_EOL;
}