1. Go to this page and download the library: Download cwola/method-interceptor 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/ */
cwola / method-interceptor example snippets
// a\MethodInterceptor;
#[MethodInterceptor\Attribute\Interceptable]
class Foo {
use MethodInterceptor\UseIntercept;
public function __construct() {
$this->__addInterceptor(new InterceptTimer, new FilterRunOnly);
$this->__addInterceptor(new InterceptGreet);
}
public function run() :bool {
$this->message('Hello');
return true;
}
protected function message(string $message) {
$this->privateMessage(
$this->bold($message)
);
}
#[MethodInterceptor\Attribute\DoNotIntercept]
protected function bold(string $message) :string {
return '**' . $message . '**';
}
private function privateMessage(string $message) {
echo $message . PHP_EOL;
}
}
class InterceptTimer implements MethodInterceptor\Contracts\Visitor {
protected array $timers = [];
public function enterMethod(string $name, ...$args) :void {
$timers[$name] = new StopWatch;
}
public function leaveMethod(string $name, ...$args) :void {
echo 'TIME : ' . $timers[$name]->stop()->time() . PHP_EOL;
}
}
class InterceptGreet implements MethodInterceptor\Contracts\Visitor {
public function enterMethod(string $name, ...$args) :void {
echo 'ENTER : ' . $name . PHP_EOL;
}
public function leaveMethod(string $name, ...$args) :void {
echo 'LEAVE : ' . $name . PHP_EOL;
}
}
class FilterRunOnly implements MethodInterceptor\Contracts\Filter {
public function test(string $name, ...$args) :bool {
return $name === 'run';
}
}
// ew Foo;
$foo->run();
// output:
//
// [ENTER] : run
// [ENTER] : message
// [ENTER] : privateMessage
// **Hello**
// [LEAVE] : privateMessage
// [LEAVE] : message
// TIME : xxx
// [LEAVE] : run
//
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.