Topic on Project:Support desk

calling functions in a hook extension

4
Lucy Tech (talkcontribs)

I'm trying to write a hook extension. I have .php file which has:

// Load this php code. 
$wgAutoloadClasses['AddImages'] = $dir . 'AddImages.body.php';
//Hooks
$wgHooks['EditPage::showEditForm:fields'][] = 'AddImages::fnAddImages';

Then in AddImages.body.php I have the class and functions
Class AddImages{
        function form(){
//some php code
}
	function fnAddImages(){
	global $wgOut, $wgHooks, $wgUser;
// some code    $call form = form(); 
$wgOut -> $call form;     
return true; 
}
}

My issue is I need to call another function eg form() within fnAddImages() but when I try to I get the message: Fatal error: Call to undefined function form();

How to I call other functions in the class besides for the 'hooked' function? Am I missing something about understanding hooks? I need to output other data (form submission) besides for just returning true on the hooked function. I thought I would do this by calling other functions within the hook. Is there a better way to do this? Thanks, I'm really stuck on this.

Bawolff (talkcontribs)

You should be able to do self::form() to call another function from the same class as your hook (statically anyways. To call non-statically use $this->form(). In your example you have the hook function being registered to be called statically, but the actual function not being declared as static). See http://www.php.net/manual/en/language.oop5.basic.php for more information.

Krinkle (talkcontribs)

While at it, you may want to rename the class to AddImagesHooks and make sure that the statically called functions actually are static:

// Load this php code. 
$wgAutoloadClasses['AddImagesHooks'] = $dir . 'AddImages.hooks.php';

//Hooks
$wgHooks['EditPage::showEditForm:fields'][] = 'AddImagesHooks::doAdd';
 
# AddImages.hooks.php:
Class AddImagesHooks {
    public static function getFormHtml() {
        // some php code
    }

    public static function doAdd() {
        global $wgOut, $wgHooks, $wgUser;

        // some code

        $form = self::getFormHtml(); 
        $wgOut->addHtml($form); 
        return true; 
    }
}
Reply to "calling functions in a hook extension"