codebird
codebird

Reputation: 397

How can I override a single function from a php custom class declared in a Wordpress parent theme?

I'm creating a child theme from a Wordpress theme. The parent theme relies heavily on a custom class which is then instantiated in the functions.php file, like so:

<?php
require_once (TEMPLATEPATH . '/advanced/specialclass.php');

$specialclass = new SpecialClass();
if(!function_exists('lets_go')) {
    function lets_go(){
        global $specialclass;
        $specialclass->start();
    }
}

lets_go();
?>

I need to override a single function from that class in my child theme, which I attempted to do by creating a functions.php file for the child theme. I tried several variations of the following:

<?php
require_once (TEMPLATEPATH . '/advanced/specialclass.php');
require_once (STYLESHEETPATH . '/advanced/specialclassextended.php');

global $specialclass;
$specialclass = new SpecialClassExtended();

function lets_go(){
    global $specialclass;
    $specialclass->start();
}

?>

Maybe my problem has something to do with not understanding PHP globals well enough, but whatever it is, I'm stuck. There are many many references to $specialclass in the parent theme files, so replacing them all by creating new template files in the child theme is impractical. I really need to just replace that global with an instance of my extended class. Is this possible? What am I doing wrong? In all my searching so far, this is the most closely related info I've found, but unfortunately it doesn't answer my specific question:

https://wordpress.stackexchange.com/questions/30728/using-classes-instead-of-global-functions-in-functions-php

Upvotes: 0

Views: 943

Answers (1)

pestilence669
pestilence669

Reputation: 5699

I do not necessarily recommend this... but, if you have administrative access to your servers and can install PECL extensions, you can use runkit_method_redefine().

Otherwise, your only viable option is to modify that theme. One thing you can do, is to simply rename the SpecialClass to something like SpecialClassBase and rename your derived to SpecialClass's original name.

If you include your own definition at the end of the file, you shouldn't need to change anything else. It should just "work."

Upvotes: 0

Related Questions