Download the PHP package ed-fruty/strange-observer without Composer
On this page you can find all versions of the php package ed-fruty/strange-observer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package strange-observer
Strange PHP observer (JFF)
Sometimes we need to listen class calling actions, but for example we haven't permissions to edit it.
Sometimes we have some instance of class only and we can't extend this class. (For example Database class. Instance was created and if we try to create new extend instance - new connection will be created. Or class can be final etc.)
But functional of this class does not satisfy to our needs.
For such cases you can use this strange observer ;)
Installation
Install package via composer
Usage
For example we have class User
with method register($attributes)
and we want to add listeners for this method
All done!
We can use $user
as previously
Ok, now we want to add validation before register
And we want to send email when registration was successfull
Now we can use
and before regitration will execute validaion, and after - sending email.
We can set priorities to the listeners
Also, we can bind new methods dynamically to the $user
We can redefine existing methods
As you can see, in the code we uses 3 types of calling methods
$user->method()
$user()->method()
-
$user(true)->method()
$user
is instanece ofInvoker
class and call$user->method()
remap to the real instance ofUser
class and can be listened$user(true)
is real instance ofUser
, calling$user(true)->method()
can not be listened$user()
is instance of observer to adding new listeners, binding new methods So, when you bind method
We can add subscriber to the
$user
callsHow to check instance ?
if ($user instanceof User === true) { // Fail. $user is instance of Fruty\Observe\Invoker } if ($user(true) instanceof User) { // Success }