Extending theme PHP class in plugin

Make your class visible to PHP after the theme has declared the parent class.

Plugins are loaded first, the theme maybe later. This can be overridden.

Theme code is available when the action after_setup_theme is fired, so you could use it like this:

add_action( 'after_setup_theme', function() {
    require 'ChildClass.php';   
});

However, if the theme loads the parent class later, you might run into the same problem: You don’t know when it is really available.

The alternative is an autloader: Do not load classes in advance. Let PHP wait instead until you create a new instance of a class, then let a registered autoloader include the proper file.

Here is an overly simple example that you can put into your plugin’s main file. Make sure you file name matches the class name, eg. ET_Builder_Module_Blurb.php.

spl_autoload_register( function( $name ) {

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

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