1

I'm upgrading several of my plugins to use PHP namespaces, so that functions, classes, constants etc are all namespaced correctly.

<?php
namespace my\plugin;

function init() { 
  ... 
}

//  Calls \my\plugin\init();
add_action('init', __NAMESPACE__.'\init');

In several places my plugin calls do_action or apply_filters so that other plugins can modify its behaviour. If these are actions or filters that are unique to my plugin, it seems to me they should be namespaced as well, like so:

do_action('\my\plugin\stuff');
$value = apply_filters('\my\plugin\value', $value);

But I don't see anybody else out there doing this. Aside from namespaces requiring PHP 5.3, is there a good reason why not? Are the \ characters going to break on some platforms or with some unusual settings?

  • 1
    Requiring 5.3 is the big reason. Php that I write that's on a server that I control is vastly different from php that I release to the greater WordPress ecosystem. I mean, as of this writing, 34.2% of WordPress sites still use php 5.2. As gross as that is, it's a reality you have to deal with if you ever want to release your code. – Andrew Bartel Dec 4 '14 at 17:53
  • That's a bit depressing. I've played this game with IE6. – Marcus Downing Dec 5 '14 at 10:18
  • Sorry, but no. PHP 5.3 came out on 30th June 2009, which is more than 5 years ago now. Any web server still running software that old on the public internet is going to have bigger problems than being able to run my little plugin, and I'll be doing them a favour by exerting a little pressure to make them upgrade or find a better host. – Marcus Downing Dec 5 '14 at 12:34
2

There is nothing technically wrong with that, but backslashes are hard to type on some keyboard layouts. On a German keyboard for example, you have to hit the right AltGr key plus \ at the same time.

German keyboard

This is not easy to type (and one of the reasons why I use an English keyboard). Use dots, colons or underscores to namespace your hooks. They are not bound to the PHP syntax anyway.

  • That means PHP namespaces are a pain for German users as well? – Marcus Downing Dec 4 '14 at 16:12
  • @MarcusDowning IDE auto-complete to the rescue. :) That’s not that difficult. But look at the positions of {, }, [ and ] – fuxia Dec 4 '14 at 16:16
  • So you'd recommend eg my:plugin:stuff? – Marcus Downing Dec 5 '14 at 12:23
  • @MarcusDowning Yes, that or my.plugin.stuff. – fuxia Dec 5 '14 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.