You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the DocBlock of the relevant registerObserver() method, it is indicated that, aside from using class strings, observers may also be registered using instantiated objects (@param object|string $class)...
The same goes if we try to register an observer object that depends on properties that cannot be arbitrarily instantiated...
$observer = newUserObserver;
$observer->logEvents = true; // has no effect
User::observe($observer);
Adding support for anonymous objects and stateful objects as observers should be reasonably straightforward, as we already support closure-based event listeners, and these can be constructed from any object.
For anonymous objects, this could be perfectly backwards-compatible, as these aren't currently supported at all.
With stateful objects, it's a little more complicated, as users may depend on the current implementation. But it may be worth looking into for an upcoming major version.
Anyone else think this is worth looking into?
If this idea gets green-lighted, I am willing to get the ball rolling with a PR with tests.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Good day Laravel community!
As you know, a model observer can be registered like so:
In the DocBlock of the relevant
registerObserver()
method, it is indicated that, aside from using class strings, observers may also be registered using instantiated objects (@param object|string $class
)...framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Lines 47 to 55 in 0bb3d76
This works fine for regular, instantiable objects for which a class can be determined.
However, it breaks (silently) when trying to register an observer object via an anonymous class, as these cannot later be instantiated...
The same goes if we try to register an observer object that depends on properties that cannot be arbitrarily instantiated...
Adding support for anonymous objects and stateful objects as observers should be reasonably straightforward, as we already support closure-based event listeners, and these can be constructed from any object.
For anonymous objects, this could be perfectly backwards-compatible, as these aren't currently supported at all.
With stateful objects, it's a little more complicated, as users may depend on the current implementation. But it may be worth looking into for an upcoming major version.
Anyone else think this is worth looking into?
If this idea gets green-lighted, I am willing to get the ball rolling with a PR with tests.
Beta Was this translation helpful? Give feedback.
All reactions