title: MediaWiki $wgUser to $this->getUser() and $context->getUser() not working
body: <p>I am learning Mediawiki and looking at some of the extensions.</p>
  
  <p><a href="https://www.mediawiki.org/wiki/Manual:$wgUser" rel="nofollow noreferrer">Manual:$wgUser</a> on mediawiki.org states the global variable <code>$wgUser</code> should not be used for new code.</p>
  
  <p><a href="https://www.mediawiki.org/wiki/Manual:RequestContext.php" rel="nofollow noreferrer">Manual:RequestContext.php</a> says the context object should be used instead, by using either <code>$this-&gt;getUser()</code> or <code>$context-&gt;getUser()</code>.</p>
  
  <p>However, when I try to use <code>$this-&gt;getUser()-&gt;getName()</code> in the extension for Who's Online I get the following error:</p>
  
  <pre><code>Fatal error: Using $this when not in object context in /home/ghsfhaco/public_html/wiki/extensions/WhosOnline/WhosOnlineHooks.php on line 19
  </code></pre>
  
  <p>And when I change it to <code>$context-&gt;getUser()-&gt;getName()</code> I get this error:</p>
  
  <pre><code>Fatal error: Call to a member function getUser() on null in /home/ghsfhaco/public_html/wiki/extensions/WhosOnline/WhosOnlineHooks.php on line 19
  </code></pre>
  
  <p>The full <a href="https://www.mediawiki.org/wiki/Extension:WhosOnline" rel="nofollow noreferrer">Extension:WhosOnline</a> can be found at Mediawiki, but here's the specific page:</p>
  
  <pre><code>    class WhosOnlineHooks {
  
      // update online data
      public static function onBeforePageDisplay() {
          global $wgUser;
  
          // write to DB (use master)
          $dbw = wfGetDB( DB_MASTER );
          $now = gmdate( 'YmdHis', time() );
  
          // row to insert to table
          $row = array(
              'userid' =&gt; $wgUser-&gt;getId(),
              'username' =&gt; $wgUser-&gt;getName(),
              'timestamp' =&gt; $now
          );
  
          $method = __METHOD__;
          $dbw-&gt;onTransactionIdle( function() use ( $dbw, $method, $row ) {
              $dbw-&gt;upsert(
                  'online',
                  $row,
                  array( array( 'userid', 'username' ) ),
                  array( 'timestamp' =&gt; $row['timestamp'] ),
                  $method
              );
          } );
  
          return true;
      }
  
      public static function onLoadExtensionSchemaUpdates( $updater ) {
          $updater-&gt;addExtensionUpdate( array( 'addTable', 'online',
              __DIR__ . '/whosonline.sql', true ) );
          return true;
      }
  }
  </code></pre>
  
  <p>How exactly should it be done?</p>
  
  <p>BTW, I'm using Mediawiki 1.28.0.</p>