1. Denis Žoljom

    Class constants. The constants that are defined using define() construct are not influenced by a namespace :)


  2. Mike Schinkel

    Hey Justin,

    It would be really good if the WordPress core team where to officially document that the \WordPress and \WP namespaces were reserved. This so developers new to namespaces would not think the best way to write code for WordPress is use one of those two _(which they might do if they are thinking their code is “for” use with WordPress….)_

    Don’t you think?


  3. Benjamin Intal

    I’ll be honest and say that this article was actually helpful for me. I didn’t bother using PHP Namespaces and kind of forgot how to use them. I’m still doing things the old 5.2 way (because I hated getting 1-star reviews from “unexpected T_FUNCTION”s errors), even if WordPress has officially dropped lower PHP versions.


  4. Jesse

    Very nice, Justin. It’s been interesting how little discussion there has been about using PHP namespaces in WordPress dev. In fact, your blog post last year is one of the few I’ve seen:


    Another detailed post by Steve Grunwell:


    I think it would be very interesting for you to write about object oriented programming and plugin/theme structure too, based on your vast experience in this space.

    It seems a lot more agencies are starting to maintain their own starter themes and starter plugins (us included) and esp. as developers try to compare WP development with e.g. Laravel, Composer, etc it might be a nice follow up to this exploration of PHP namespaces…


    • David Dashifen Kees

      I’ve been playing around with fully OOP WordPress development for a few years. It’s a little weird, but it can be done. I’d love to compare/contrast my work against others’. +1 to this idea from me, for what it’s worth.


  5. Code Potent

    Great article, Justin.

    Just wanted to add a note regarding constants and namespaces. You actually can use the define function to create a namespaced constant; you just have to indicate the namespace (thanks, magic constants!) For example, the following two lines create an equivalent, namespaced constant.

    const MY_CONSTANT = 'some value';


    define(__NAMESPACE__.'\MY_CONSTANT', 'some value');

    …notably, the const declaration cannot use functions on the right side of the operand… you can still concatenate a string, but, no functions. Conversely, using the define() function, does allow you to use functions on the right side of the operand.



  6. Code Potent

    Aside: it looks like the double-underscores on either side of the keyword NAMESPACE in my previous comment were translated as markdown…in case anyone tries and it seems to not work. :)


Comments are closed.

%d bloggers like this: