Wikimedia Developer Support

BeforePageDisplay Hook Error

hooks
extensions

#1

Error

Fatal error: Uncaught TypeError: Argument 1 passed to MediaWiki\Extension\DiscordAds\Hooks::onBeforePageDisplay() must be an instance of MediaWiki\Extension\DiscordAds\OutputPage, object given, called in /var/lib/mediawiki/includes/Hooks.php on line 177 and defined in /var/lib/mediawiki/extensions/DiscordAds/src/Hooks.php:30
Stack trace:
#0 /var/lib/mediawiki/includes/Hooks.php(177): MediaWiki\Extension\DiscordAds\Hooks::onBeforePageDisplay(Object(OutputPage), Object(SkinDiscord))
#1 /var/lib/mediawiki/includes/Hooks.php(237): Hooks::callHook('BeforePageDispl...', Array, Array, NULL, 'MediaWiki\\Exten...') #2 /var/lib/mediawiki/includes/OutputPage.php(2385): Hooks::runWithoutAbort('BeforePageDispl...', Array)
#3 /var/lib/mediawiki/includes/exception/MWExceptionRenderer.php(135): OutputPage->output()
#4 /var/lib/mediawiki/includes/exception/MWExceptionRenderer.php(54): MWExceptionRenderer::reportHTML(Object(TypeError))
#5 /var/lib/mediawiki/includes/exception/MWExceptionHandler.php(75): MWExceptionRenderer::output(Object(T in /var/lib/mediawiki/extensions/DiscordAds/src/Hooks.php on line 30`

Case

I’m writing an extension and I get the error above when I add the onBeforePageDisplay to extension.json.

Hooks Class

This is the class file that is being referenced in the errors above. Right now it’s class is called Hooks. However it gives a different error when renaming just the class name.

Two example errors when renaming the class to DiscordAdsHook and DiscordHooks can be seen below.

**Fatal error** : Cannot declare class MediaWiki\Extension\DiscordAds\DiscordAdsHooks, because the name is already in use in **/var/lib/mediawiki/extensions/DiscordAds/src/Hooks.php** on line **25**

**Fatal error** : Cannot declare class MediaWiki\Extension\DiscordAds\DiscordHooks, because the name is already in use in **/var/lib/mediawiki/extensions/DiscordAds/src/Hooks.php** on line **25**

What do these errors mean? And how do I solve this problem?


#2

You can see from the stack trace that an OutputPage object is passed in the first argument, but the error message says your method expects a MediaWiki\Extension\DiscordAds\OutputPage object. So the method is defined incorrectly. You’ll probably want \OutputPage instead of OutputPage to avoid the default namespace for that file being prepended to the class name.

The other errors just say you have two files trying to define the same class.


#3

Thanks, this solved the issue!