PropertyObserver
A class that implements PropertyObserver is told about every read and every write of its own properties.
You write the two members onPropertyGet and onPropertySet once, and they are called for every property the class declares. Each is handed the property's name and the value that has just settled: the value the reader received, or the value that was stored. Both return nothing, so an observer reports a change and never changes it. Novis does not recognize the methods __get and __set.
Good to know: the report always comes second. A property with a set block of its own runs that block first, so the observer is told what was really stored and not what the caller passed. A class that does not implement the interface pays nothing for one. And an observer that writes a property of its own class is called again for that write, so its body has to be the thing that stops.
Examples
An audit trail of every change
<?nvs// A customer record that logs every change made to it. The logging code is in one method.// Every change to a property adds an entry. A property you add later is logged too.
// The entries are stored outside the record. So adding an entry is not a// change to the customer.final class Audit{ public static array<tainted string> $entries = [];}
class Customer implements PropertyObserver{ public string $name = 'unknown'; public string $email = ''; public uint $orders = 0;
public function onPropertyGet(string $name, mixed $value): void { }
// This method runs after the value is stored, so `$value` is the new value. // A class must have both methods, even if it does nothing on a read. public function onPropertySet(string $name, mixed $value): void { Audit::$entries[] = $name . ' is now ' . $value; }}
var $ada = new Customer();$ada->name = 'Ada Lovelace';$ada->email = 'ada@example.com';
// A correction and a new order, later in the day. Neither line calls any// logging code.$ada->email = 'ada.lovelace@example.com';$ada->orders = $ada->orders + 1;
echo "properties changed, oldest first:\n";foreach (Audit::$entries as tainted string $field) { echo ' ', $field, "\n";}echo 'the record now: ', $ada->name, ' <', $ada->email, '> ', $ada->orders, " orders\n";properties changed, oldest first: name is now Ada Lovelace email is now ada@example.com email is now ada.lovelace@example.com orders is now 1the record now: Ada Lovelace <ada.lovelace@example.com> 1 orders