Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Different callables in WordPress hooks.
<?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().
*/
@szepeviktor

This comment has been minimized.

Copy link
Owner Author

szepeviktor commented Nov 17, 2019

The usual WordPress "class" is really just a bunch of functions.

$diContainerConstructorOnly = function () { new UsualWordPressClass(); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.