10 Comments

  1. Denis Žoljom
    · Reply

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

    Report

  2. Mike Schinkel
    · Reply

    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?

    Report

  3. Benjamin Intal
    · Reply

    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.

    Report

  4. Jesse
    · Reply

    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:

    http://justintadlock.com/archives/2018/12/14/php-namespaces-for-wordpress-developers

    Another detailed post by Steve Grunwell:

    https://stevegrunwell.com/blog/php-namespaces-wordpress/

    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…

    Report

    • David Dashifen Kees
      · Reply

      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.

      Report

  5. Code Potent
    · Reply

    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';
    

    or

    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.

    Cheers!

    Report

  6. Code Potent
    · Reply

    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. :)

    Report

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: