Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hooks() function #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

jesobreira
Copy link

I've added this at the end of the file:

function hooks() {
    global $hooks;
    return $hooks;
}

So instead of:

...my_custom_method_somewhere() {
   global $hooks;
   $hooks->do_action('my_custom_hook');
}

I can do just:

...my_custom_method_somewhere() {
   hooks()->do_action('my_custom_hook');
}

As well as, instead:

...blah() {
   global $hooks;
   $hooks->add_action('my_custom_hook', function() {
      echo 'blah';
   });
}

I can just do:

...blah() {
   hooks()->add_action('my_custom_hook', function() {
      echo 'blah';
   });
}

@BassemN
Copy link

BassemN commented Jan 18, 2016

+1

@Firestorm-Graphics
Copy link

heres a better way:

`function add_action($hook, $function) {
global $hooks;
return $hooks->add_action($hook, $function);
}

function has_action($hook, $function) {
global $hooks;
return $hooks->has_action($hook, $function);
}

function do_action($hook, $args = '') {
global $hooks;
return $hooks->do_action($hook, $args);
}

function remove_action($hook, $function, $priority = '') {
global $hooks;
return $hooks->remove_action($hook, $function, $priority);
}

function remove_all_actions($hook, $priority = '') {
global $hooks;
return $hooks->remove_all_actions($hook, $priority);
}`

@jesobreira
Copy link
Author

jesobreira commented Jul 9, 2017

Hi, @Firestorm-Graphics . Thanks for your comment!

Personally I'm not a huge fan of dropping several functions with not so obvious names on the main namespace. If I used wrappers for PHP Hooks' actions as you did, I would at least make them static methods inside a class. The advantage of using the hooks()->action approach is that the original methods of the lib are visible, and not wrapped, to the other namespaces. But as I said, it's something very personal :)

@voku
Copy link

voku commented Jul 25, 2017

@jesobreira you can also add namespace for simple functions

namespace hooks {
  function add_action($hook, $function) {
    $hooks = Hooks::getInstance();

    return $hooks->add_action($hook, $function);
  }

  //... 
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants