1

I am building a plugin that I hope to eventually put on Envato CodeCanyon.

Currently I am creating functions of the form mypluginname_action. This is quickly becoming silly as I have to do that a lot. I'm thinking of using PHP namespaces, but I'm worried that there might be a reason why I shouldn't.

  1. Are namespaces compatible with WordPress and Envato's coding standards?
  2. Can I use namespaces in the PHP versions that I should be supporting?
  3. Are there any professional theme/plugin makers who make use of the namespace feature? If not, why not? Are there any common pitfalls with using namespaces?

I am well aware that I can research all of this myself, and of course I will, but as I'm early in development, at this point I'm hoping for a quick answer from more experienced people who might have already looked into this.

I appreciate any thoughts on this.

5

You can totally use namespaces in your project/theme/plugin!

The only drawback is that you introduce a PHP 5.3 dependency, while WordPress itself only requires 5.2.4.

I guess in 2015 this is not a big issue, even when distributing a plugin to a wide community (opensource or envato).

Roots team is using namespaces in their themes, plugins, etc: https://github.com/roots/sage

  • Thanks. I believe I will not use namespaces after all. After all, it's not like namespaces would increase code readability, it would just be me using a language feature that is neat. – alexg Nov 8 '15 at 14:42
2

It's almost four years after I asked this question. For anyone reading this now, namespaces are officially supported. From the 5.2 release notes:

The minimum supported PHP version is now 5.6.20. As of WordPress 5.2, themes and plugins can safely take advantage of namespaces, anonymous functions, and more!

  • 1
    Thank you, I had been looking for this info for an hour, and your answer is the first definite and specific info I have found today in Jul 2019. WP Developer Handbook keeps the instruction on the ridiculous 'prefixing' with no mention of namespacing. – alx Jul 6 at 11:11
1

In a way you are over complicating things for yourself, but OTOH you are being too simplistic.

You should not sell software that you can not test. If you claim that your software works on all versions of wordpress starting with 3.5 and php starting with 5.3 you might be ok if you do not test all the combinations, but if some version will prove not to work with your code then you will have to spend unexpected time to fix it. The extra time might be the difference between a profit and lose, and while you are working to fix it, your reputation might be irrecoverably tarnished.

Right now wordpress recommends 5.6 so start with it, write good unit tests and once your code works on 5.6 you can try to run it on 5.5 etc and see where it breaks. I would say that at this stage don't bother to fix the error, and wait until there is a demand to compatibility with that version, and how big it is, to decide if you actually want to put the effort into supporting it.

  • Thanks, I appreciate your thoughts. You're right, namespaces seem like too much trouble for no real benefit. In fact I am planning to throroughly test with various setups once I have a release candidate. I'm thinking of perhaps using docker, once I get a proper dockerfile, I should be able to quickly test various combinations of PHP and WP versions. That's a thought for another day I guess. Again, thanks :) – alexg Nov 8 '15 at 14:47
  • Followup for anyone interested, 7 moths later: What a long, strange trip it's been! I have now written an article on how I did testing with docker. alexgeorgiou.gr/test-wordpress-with-docker I did not use the PHP namespaces feature in my code but my plugin is one big class file so all names fall under that class. – alexg May 17 '16 at 10:56

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.