Skip to content

Instantly share code, notes, and snippets.

@ozh
Created March 2, 2021 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozh/bf5a3c6e03f0fef89d849cb76fae49d3 to your computer and use it in GitHub Desktop.
Save ozh/bf5a3c6e03f0fef89d849cb76fae49d3 to your computer and use it in GitHub Desktop.
All possible hook syntaxes in YOURLS
<?php
// Load YOURLS
require_once( __DIR__.'/includes/load-yourls.php' );
?>
<pre>
<?php
// Simple hook with string <function name>
function my_callback_function($in) {
return "$in 1 ";
}
yourls_add_filter('my_hook_test', 'my_callback_function');
// Hook with array(<class name>, <method>)
class A {
static function my_callback_function($in) {
return "$in 2 ";
}
}
yourls_add_filter('my_hook_test', array('A','my_callback_function'));
// Hook with string "<class name>::<method>"
class B {
static function my_callback_function($in) {
return "$in 3 ";
}
}
yourls_add_filter('my_hook_test', 'B::my_callback_function');
// Hook with array(<class instance>, <method>)
class C {
static function my_callback_function($in) {
return "$in 4 ";
}
}
$obj = new C();
yourls_add_filter('my_hook_test', array($obj, 'my_callback_function'));
// Hooks with array(<class name>, <method>) with class inheritance
class D {
public static function my_callback_function($in) {
return "$in 5 ";
}
}
class E extends D {
public static function my_callback_function($in) {
return "$in 6 ";
}
}
yourls_add_filter('my_hook_test', array('E', 'parent::my_callback_function'));
yourls_add_filter('my_hook_test', array('E', 'my_callback_function'));
// Hook with object implementing __invoke()
class F {
public function __invoke($in) {
return "$in 7 ";
}
}
$f = new F();
yourls_add_filter('my_hook_test', $f);
// Hook with closure
$my_callback_function = function($in) {
return "$in 8 ";
};
yourls_add_filter('my_hook_test', $my_callback_function);
// Now apply filter
var_dump(yourls_apply_filter('my_hook_test', 'hello'));
// Result : string 'hello 1 2 3 4 5 6 7 8 ' (length=29)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment