public function test_pass_param() { Hook::clear(); $return = null; Hook::on('param_test', function ($params) use(&$return) { $return = $params['return']; }); Hook::call('param_test', array('return' => 'test param')); $this->assertEquals('test param', $return); }
<?php require 'vendor/autoload.php'; use Ryanhs\Hook\Hook; Hook::on('test', function () { return 1; }); Hook::on('test', function () { return '3'; }); Hook::on('test', function () { return 5; }); echo 'call_yield:' . PHP_EOL . PHP_EOL; foreach (Hook::call_yield('test') as $return) { echo $return . PHP_EOL; } echo PHP_EOL . str_repeat('=', 10) . PHP_EOL; echo 'call: (return as array)' . PHP_EOL . PHP_EOL; var_dump(Hook::call('test'));
<?php require 'vendor/autoload.php'; use Ryanhs\Hook\Hook; use Ryanhs\GAToolkit\Toolkit; use Ryanhs\GAToolkit\Chromosome\SimpleString; $ga = new Toolkit(); $ga->goal = isset($argv['1']) ? $argv['1'] : 'test'; //$ga->chromosome = '\Ryanhs\GAToolkit\Chromosome\SimpleStringOptimized'; $ga->chromosome = '\\Ryanhs\\GAToolkit\\Chromosome\\SimpleStringStd'; $ga->selection = 0.9; $ga->mutation = 0.1; //$ga->max_generation = 20; $ga->max_population = 20; Hook::on(Toolkit::HOOK_REGENERATION, function ($params) { $ga = $params['object']; echo 'Generation #' . $ga->generation . ' -> ' . $ga->solution . PHP_EOL; time_nanosleep(0, 50000000); }); Hook::on(Toolkit::HOOK_FINISH_GOAL, function ($params) { $ga = $params['object']; echo 'Solution get on generation #' . $ga->generation . ' -> ' . $ga->solution . PHP_EOL; }); Hook::on(Toolkit::HOOK_FINISH_NOGOAL, function ($params) { $ga = $params['object']; echo 'No Solution! reach max generation #' . $ga->generation . PHP_EOL; }); $ga->run();
<?php require 'vendor/autoload.php'; use Ryanhs\Hook\Hook; Hook::on('test', function () { echo 'hook 1!' . PHP_EOL; }); Hook::on('test', function () { echo 'hook 2!' . PHP_EOL; }); Hook::call('test');
<?php require 'vendor/autoload.php'; use Ryanhs\Hook\Hook; Hook::on('init', function () { echo 'hello world!' . PHP_EOL; }); Hook::call('init');
<?php require 'vendor/autoload.php'; use Ryanhs\Hook\Hook; Hook::on('foo', function () { return 1; }); Hook::on('bar', function () { return '3'; }); Hook::on('foo', function () { return 5; }); // clear hook "bar" Hook::clear('bar'); echo PHP_EOL . 'hook "bar":' . PHP_EOL; var_dump(Hook::call('bar')); echo PHP_EOL . 'hook "foo":' . PHP_EOL; var_dump(Hook::call('foo'));
<?php require 'vendor/autoload.php'; use Ryanhs\Hook\Hook; Hook::on('finish', function () { echo 'job done!' . PHP_EOL; }); Hook::on('process', function () { echo 'in progress!' . PHP_EOL; }); Hook::on('init', function () { echo 'job retrieved!' . PHP_EOL; }); class Job { public function __construct() { Hook::call('init'); } public function start() { Hook::call('process'); // some logic here // some logic here // some logic here Hook::call('finish'); } } $job = new Job(); $job->start();