PHP hates globals
[lhc/web/wiklou.git] / docs / globals.txt
1 globals.txt
2
3 Globals are evil. The original MediaWiki code relied on
4 globals for processing context far too often. MediaWiki
5 development since then has been a story of slowly moving
6 context out of global variables and into objects. Storing
7 processing context in object member variables allows those
8 objects to be reused in a much more flexible way. Consider
9 the elegance of:
10
11 # Generate the article HTML as if viewed by a web request
12 $article = new Article( Title::newFromText( $t ) );
13 $article->view();
14
15 versus
16
17 # Save current globals
18 $oldTitle = $wgTitle;
19 $oldArticle = $wgArticle;
20
21 # Generate the HTML
22 $wgTitle = Title::newFromText( $t );
23 $wgArticle = new Article;
24 $wgArticle->view();
25
26 # Restore globals
27 $wgTitle = $oldTitle
28 $wgArticle = $oldArticle
29
30 Some of the current MediaWiki developers have an idle
31 fantasy that some day, globals will be eliminated from
32 MediaWiki entirely, replaced by an application object which
33 would be passed to constructors. Whether that would be an
34 efficient, convenient solution remains to be seen, but
35 certainly PHP 5 makes such object-oriented programming
36 models easier than they were in previous versions.
37
38 For the time being though, MediaWiki programmers will have
39 to work in an environment with some global context. At the
40 time of writing, 418 globals were initialised on startup by
41 MediaWiki. 304 of these were configuration settings, which
42 are documented in DefaultSettings.php. There is no
43 comprehensive documentation for the remaining 114 globals,
44 however some of the most important ones are listed below.
45 They are typically initialised either in index.php or in
46 Setup.php.
47
48
49 $wgOut
50 OutputPage object for HTTP response.
51
52 $wgUser
53 User object for the user associated with the current
54 request.
55
56 $wgTitle
57 Title object created from the request URL.
58
59 $wgLang
60 Language object selected by user preferences
61
62 $wgContLang
63 Language object associated with the wiki being
64 viewed.
65
66 $wgArticle
67 Article object corresponding to $wgTitle.
68
69 $wgLinkCache
70 LinkCache object.
71
72 $wgParser
73 Parser object. Parser extensions register their
74 hooks here.
75
76 $wgLoadBalancer
77 A LoadBalancer object, manages database connections.