I'm learning WordPress plugin development using OOP approach. In most resources before declaring a class there is usually a check if class exists class_exists().

Is it a good approach to check this or can it be omitted?

Also, I was wondering if it's a good approach to check in one block if none of plugin's classes exist. Even if one class already exists, the whole plugin can't work correctly as it can't re-declare a class. So, instead having class_exists() through the code, I was wondering if it's better to have it in one block when activating a plugin, something like:

if ( class_exists( 'Class_Name_One' )
|| class_exists( 'Class_Name_Two' )
|| class_exists( 'Class_Name_Three' )
|| class_exists( 'Class_Name_Four' ) ) {



Any advice and feedback is hugely appreciated.

EDIT If there is a conflict with another plugin that declares the same class names, I want to deactivate my plugin and display admin notice.

1 Answer 1


Use an auto-loader and let PHP resolve that problem for you. Then only one class will be loaded, and you don’t have to worry about doubled declarations.


I have an (unfinished) plugin Post Notes with the following file structure:

- plugin.php
- php/
   - Nonce_Validator.php
   - Save_Post_Request_Validator.php
   - Textarea.php
   - … and more

This is a very simple plugin, so all files use the same namespace Wpkrauts. In plugin.php, I register a simple auto-loading routine:

spl_autoload_register( function( $name ) {

    $name = ltrim( $name, '\\' );

    if ( 'Wpkrauts' !== strtok( $name, '\\' ) )

    $file = strtok( '.' );

    $path = __DIR__ . "/php/$file.php";

    if ( file_exists( $path ) )
        require $path;

// Now start.
is_admin() && new Post_Notes( 'wpkrauts_post_notes' );

You can do the same without using namespaces (not recommended, exept when you have to support insecure PHP versions like 5.2).

In PHP 5.2 the SPL library can be turned off, but that was never an issue in the plugins I know. Our company distributes plugins like BackWPup with more than a million downloads, and there was never a single compliant about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.