Skip to content

Instantly share code, notes, and snippets.

@szepeviktor
Last active November 17, 2019 04:26
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 szepeviktor/da15f96d5375c8c3ad35f67a7f70cc17 to your computer and use it in GitHub Desktop.
Save szepeviktor/da15f96d5375c8c3ad35f67a7f70cc17 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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