4 improved formatting
source | link

OnOne could easily adapt this for use without namespaces. Assuming you prefix your plugin's/theme's classes uniformly, you could just test for that prefix. Then use underscores in the class name to as placeholders for directory separators. If you're using a lot of classes, you'll likely want to use some sort of classmap autoloader.

If you always put your hooks into classes, it's easier. The standard create instance of a class and all all hooks in the constructor with $this works fine.

If you use static methods and the like, as I'm wont I want to do, You'llyou'll need to pass the fully qualified class name as the first argument of the array. That's a lot of work, so you can just use the magic __CLASS__ constant or get_class.

On could easily adapt this for use without namespaces. Assuming you prefix your plugin's/theme's classes uniformly, you could just test for that prefix. Then use underscores in the class name to as placeholders for directory separators. If you're using a lot of classes, you'll likely want to use some sort of classmap autoloader.

If you always put your hooks into classes, it's easier. The standard create instance of a class and all all hooks in the constructor with $this works fine.

If you use static methods and the like, as I'm wont to do, You'll need to pass the fully qualified class name as the first argument of the array. That's a lot of work, so you can just use the magic __CLASS__ constant or get_class.

One could easily adapt this for use without namespaces. Assuming you prefix your plugin's/theme's classes uniformly, you could just test for that prefix. Then use underscores in the class name to as placeholders for directory separators. If you're using a lot of classes, you'll likely want to use some sort of classmap autoloader.

If you always put your hooks into classes, it's easier. The standard create instance of a class and all hooks in the constructor with $this works fine.

If you use static methods like I want to do, you'll need to pass the fully qualified class name as the first argument of the array. That's a lot of work, so you can just use the magic __CLASS__ constant or get_class.

3 added 1 characters in body
source | link
<?php
namespace WPSE\SomeNameSpace;

// root namespace
define('WPSE_63668_PATH'WPSE_63668_PATH', plugin_dir_path(__FILE__));

// in the current namespace
define(__NAMESPACE__ . '\\PATH', plugin_dir_path(__FILE__));
<?php
namespace WPSE\SomeNameSpace;

// root namespace
define('WPSE_63668_PATH, plugin_dir_path(__FILE__));

// in the current namespace
define(__NAMESPACE__ . '\\PATH', plugin_dir_path(__FILE__));
<?php
namespace WPSE\SomeNameSpace;

// root namespace
define('WPSE_63668_PATH', plugin_dir_path(__FILE__));

// in the current namespace
define(__NAMESPACE__ . '\\PATH', plugin_dir_path(__FILE__));
2 added 917 characters in body
source | link

Defines

This is more general PHP, but it bit me, so here it is.

You may want to define things you'll use often, like the path to your plugin. Using the define statement puts things in the root namespace unless you explicitly pass the namespace into the first argument of define.

<?php
namespace WPSE\SomeNameSpace;

// root namespace
define('WPSE_63668_PATH, plugin_dir_path(__FILE__));

// in the current namespace
define(__NAMESPACE__ . '\\PATH', plugin_dir_path(__FILE__));

You can also use the const keyword on in the root level of a file with PHP 5.3 plus. constss are always in the current namespace, but are less flexible that a define call.

<?php
namespace WPSE\SomeNameSpace;

// in the current namespace
const MY_CONST = 1;

// this won't work!
const MY_PATH = plugin_dir_path(__FILE__);

Defines

This is more general PHP, but it bit me, so here it is.

You may want to define things you'll use often, like the path to your plugin. Using the define statement puts things in the root namespace unless you explicitly pass the namespace into the first argument of define.

<?php
namespace WPSE\SomeNameSpace;

// root namespace
define('WPSE_63668_PATH, plugin_dir_path(__FILE__));

// in the current namespace
define(__NAMESPACE__ . '\\PATH', plugin_dir_path(__FILE__));

You can also use the const keyword on in the root level of a file with PHP 5.3 plus. constss are always in the current namespace, but are less flexible that a define call.

<?php
namespace WPSE\SomeNameSpace;

// in the current namespace
const MY_CONST = 1;

// this won't work!
const MY_PATH = plugin_dir_path(__FILE__);
1
source | link
    Post Made Community Wiki by chrisguitarguy