Last active
November 17, 2019 04:26
-
-
Save szepeviktor/da15f96d5375c8c3ad35f67a7f70cc17 to your computer and use it in GitHub Desktop.
Different callables in WordPress hooks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Different callables in WordPress hooks. | |
* | |
* @see https://www.php.net/manual/en/language.types.callable.php | |
*/ | |
function callableFunction() { echo "I'm the callableFunction.\n"; } | |
class callableObject | |
{ | |
public function __construct() { echo "I'm callableObject's constructor.\n"; } | |
public function normalMethod() { echo "I'm callableObject's normalMethod().\n"; } | |
public static function staticMethod() { echo "I'm callableObject::staticMethod().\n"; } | |
} | |
$anonymousFunction = function () { echo "I'm an anonymous function.\n"; }; | |
$diContainer = function () { (new callableObject())->normalMethod(); }; | |
// Adding hooks. | |
add_action('init', 'callableFunction', 11, 0); | |
add_action('init', [new callableObject(), 'normalMethod'], 12, 0); | |
add_action('init', ['callableObject', 'staticMethod'], 13, 0); | |
add_action('init', $anonymousFunction, 14, 0); | |
add_action('init', $diContainer, 15, 0); | |
echo "All hooks added.\n----------------\n"; | |
/* | |
OUTPUT | |
====== | |
I'm callable_object's constructor. | |
All hooks added. | |
---------------- | |
I'm the callableFunction. | |
I'm callableObject's normalMethod(). | |
I'm callableObject::staticMethod(). | |
I'm an anonymous function. | |
I'm callableObject's constructor. | |
I'm callableObject's normalMethod(). | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The usual WordPress "class" is really just a bunch of functions.