1. Go to this page and download the library: Download ailixter/gears-watchedcall library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ailixter / gears-watchedcall example snippets
use Ailixter\Gears\CallWatching;
use const Ailixter\Gears\WatchedCall\BEFORE_CALL;
use const Ailixter\Gears\WatchedCall\AFTER_CALL;
class Example
{
use CallWatching;
/**
* @access public
* via private modifier
*/
private function watchedMethod($arg1, $arg2)
{
return $arg1 + $arg2;
}
}
$example = new Example;
$example->attachToWatch(BEFORE_CALL, 'watchedMethod', function (callable $callable, array $args) {
// $callable is guaranteed to be in form [object, method]
[$object, $method] = $callable;
// inspect $args
// ...
// do something else
});
$result = $example->watchedMethod(2, 3); // 5
$example->attachToWatch(BEFORE_CALL, 'watchedMethod', function (callable $callable) {
if (!User::canAccess($callable)) {
throw new UserAccessException;
}
});
$result = $example->watchedMethod(2, 3); // exception
$example->attachToWatch(BEFORE_CALL, 'watchedMethod', function (callable &$callable) {
if (!User::canAccess($callable)) {
// just return something acceptable
$callable = function () { return null; }
}
});
$result = $example->watchedMethod(2, 3); // null
$example->attachToWatch(BEFORE_CALL, 'unknownMethod', function (callable &$callable) {
// just return something acceptable
$callable = function () { return null; }
});
$result = $example->unknownMethod(2, 3); // null
class Delegate
{
public function watchedMethod($arg1, $arg2)
{
return $arg1 + $arg2;
}
}
class Example
{
use CallWatching;
private $delegate;
public function __construct(Delegate $delegate)
{
$this->delegate = $delegate;
}
/**
* note overriding
*/
protected function getWatchedObject()
{
return $this->delegate;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.