Manual:$wgUser
In most cases $wgUser should not be used in new code. If it is necessary to get a global User object, consider calling RequestContext::getMain()->getUser() instead. |
<translate> [[<tvar|1>Special:MyLanguage/Manual:Global object variables</>|Global object]]:</translate> $wgUser | |
---|---|
User object for the user associated with the current request | |
<translate> Class:</translate> | User |
<translate> Located in:</translate> | User.php |
Details
The User object encapsulates the state of the user viewing/using the site. The User object can be queried for things like the user's settings, name, etc. It handles the details of getting and saving to the "user" table of the database, and dealing with sessions and cookies.
Client classes use the getXXX()
functions to access these fields.
These functions do all the work of determining whether the user is logged in, whether the requested option can be satisfied from cookies or whether a database query is needed.
Most of the settings needed for rendering normal pages are set in the session to minimize use of the database.
$wgUser
is initialized towards the end of setup.
Some hooks are called earlier than that and should avoid using it.
When in doubt use $wgUser->isSafeToLoad()
to check (you can always rely on it being a User
object, but it will not refer to the right user when trying to access it early).