Jump to content

Topic on Project:Support desk

Hooks Definitions are confusing to me

8
71.178.60.68 (talkcontribs)

I was looking at the ParserFirstCallInit hook, and I think it is confusing, unless someone can explain the notation. I see something like this:

function fnMyHook( &$parser ) {
    ...
}
$wgHooks['ParserFirstCallInit'][] = 'MyExtensionHooks::someExample';

Shouldn't the second line be:

$wgHooks['ParserFirstCallInit'][] = 'fnMyHook';

I really don't understand how they tie together if I use someExample. Wouldn't I also have to define someExample and have it use the same function arguments and call fnMyHook?

Thanks.

Krinkle (talkcontribs)

No, you shouldn't create a global function for each hook. The "right way" (anno 2012) would be to have a *.hooks.php file in your extension folder, in which you create a class with public static methods. Then in your main extension php file:

$wgAutoloadClasses['VectorHooks'] = dirname( __FILE__ ) . '/Vector.hooks.php';

$wgHooks['GetPreferences'][] = 'VectorHooks::getPreferences';
$wgHooks['BeforePageDisplay'][] = 'VectorHooks::beforePageDisplay';

Look at the files in mediawiki/extensions/Vector.git as example.

88.130.124.118 (talkcontribs)

Basically the IP who asks the question is right: The definition inside the "$wgHooks['ParserFirstCallInit'][] = " line must fit to the function name. In the example, this should also be the case, but is not.

Either the second line should be

$wgHooks['ParserFirstCallInit'][] = 'fnMyHook';

or the hook function should be

class MyExtensionHooks {
 function someExample() {
 // ...your code...
 return true;
 }
}
Krinkle (talkcontribs)
88.130.110.219 (talkcontribs)

Thanks for the fix! I also tried that but did not really step through how all these templates relate to each other. :-)

209.29.183.172 (talkcontribs)
88.130.110.219 (talkcontribs)

You mean the class name, which is present in the line "$wgHooks['ParserFirstCallInit'][] = ...", but not present in the function definition? Right, it should be at both places, or at none. Having it at both would blow up all the function definitions, so I have just taken it out.

Krinkle (talkcontribs)
Reply to "Hooks Definitions are confusing to me"