ns-rx / poetwp

0 stars 0 forks source link

Use namespaces #30

Open ns-rx opened 2 months ago

ns-rx commented 2 months ago
          Rather than function prefixes (which itself is better than nothing), consider learning and using namespaces. Imagine this file like:
namespace PoetWP\Admin;

add_action( 'add_meta_boxes', __NAMESPACE__ . '\\add_meta_boxes' );
function add_meta_boxes() {
    ...
}

...

The fully qualified name for this function is PoetWP\Admin\add_meta_boxes, but we can use the __NAMESPACE__ (which means "the current namespace of this file") constant to streamline it (in case the namespace ever changes).

Function prefixes are the poor-mans version of namespaces. Namespaces typically have the format of Vendor/Project, so if you look at some of mine, it would be GaryJones\Foo, or Gamajo\MyProject, or Automattic\CoAuthorsPlus, etc. While I don't see a PoetWP in the WPORG repo, it's not out of the realm of possibility that someone else could also create a plugin with the same name, but they wouldn't create it under the Redonomics / Natalie / NS-RX vendor name.

namespace NatalieS\PoetWP\Admin;

add_action( 'add_meta_boxes', __NAMESPACE__ . '\\add_meta_boxes' );
function add_meta_boxes() {
    ...
}

...

You can look at https://packagist.org/?query=natalie to see what existing vendor names there are that you'd want to avoid clashing with your package vendor name.

_Originally posted by @GaryJones in https://github.com/ns-rx/poetwp/pull/13#discussion_r1730395774_