1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Tests\Compiling\Statements; |
4
|
|
|
|
5
|
|
|
class UnexpectedUseOfThis |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @return string |
9
|
|
|
*/ |
10
|
|
|
public function thisAsArgument($this) |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
return 'Cannot use $this as parameter'; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public function thisInCatch() |
19
|
|
|
{ |
20
|
|
|
try { |
21
|
|
|
throw new \LogicException(); |
22
|
|
|
} catch (\Exception $this) { |
23
|
|
|
return 'Fatal error: Cannot re-assign $this'; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function thisAsLoopVariable() |
31
|
|
|
{ |
32
|
|
|
foreach (['foo'] as $this) { |
33
|
|
|
return 'Fatal error: Cannot re-assign $this'; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function thisAsStaticVariable() |
41
|
|
|
{ |
42
|
|
|
static $this; |
43
|
|
|
|
44
|
|
|
return 'Fatal error: Cannot use $this as static variable'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function thisAsGlobalVariable() |
51
|
|
|
{ |
52
|
|
|
global $this; |
53
|
|
|
|
54
|
|
|
return 'Fatal error: Cannot use $this as global variable'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function unsetThis() |
61
|
|
|
{ |
62
|
|
|
unset($this); |
63
|
|
|
|
64
|
|
|
return 'Fatal error: Cannot unset $this'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function OtherAsArgument($a) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return 'Ok'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function OtherInCatch() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
throw new \LogicException(); |
82
|
|
|
} catch (\Exception $a) { |
83
|
|
|
return 'Ok'; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function OtherAsLoopVariable() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
foreach (['foo'] as $a) { |
93
|
|
|
return 'Ok'; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function OtherAsStaticVariable() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
static $a; |
103
|
|
|
|
104
|
|
|
return 'Ok'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function OtherAsGlobalVariable() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
global $a; |
113
|
|
|
|
114
|
|
|
return 'Ok'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function unsetOther() |
121
|
|
|
{ |
122
|
|
|
$a = 1; |
123
|
|
|
unset($a); |
124
|
|
|
|
125
|
|
|
return 'Ok'; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
?> |
129
|
|
|
---------------------------- |
130
|
|
|
[ |
131
|
|
|
{ |
132
|
|
|
"type":"unexpected_use.this", |
133
|
|
|
"message":"Method thisAsArgument can not have a parameter named \"this\".", |
134
|
|
|
"file":"UnexpectedUseOfThis.php", |
135
|
|
|
"line":9 |
136
|
|
|
}, |
137
|
|
|
{ |
138
|
|
|
"type":"unexpected_use.this", |
139
|
|
|
"message":"Catch block can not have a catch variable named \"this\".", |
140
|
|
|
"file":"UnexpectedUseOfThis.php", |
141
|
|
|
"line":21 |
142
|
|
|
}, |
143
|
|
|
{ |
144
|
|
|
"type":"unexpected_use.this", |
145
|
|
|
"message":"Foreach loop can not use a value variable named \"this\".", |
146
|
|
|
"file":"UnexpectedUseOfThis.php", |
147
|
|
|
"line":31 |
148
|
|
|
}, |
149
|
|
|
{ |
150
|
|
|
"type":"unexpected_use.this", |
151
|
|
|
"message":"Can not declare a static variable named \"this\".", |
152
|
|
|
"file":"UnexpectedUseOfThis.php", |
153
|
|
|
"line":41 |
154
|
|
|
}, |
155
|
|
|
{ |
156
|
|
|
"type":"unexpected_use.this", |
157
|
|
|
"message":"Can not declare a global variable named \"this\".", |
158
|
|
|
"file":"UnexpectedUseOfThis.php", |
159
|
|
|
"line":51 |
160
|
|
|
}, |
161
|
|
|
{ |
162
|
|
|
"type":"unexpected_use.this", |
163
|
|
|
"message":"Can not unset $this.", |
164
|
|
|
"file":"UnexpectedUseOfThis.php", |
165
|
|
|
"line":61 |
166
|
|
|
} |
167
|
|
|
] |
168
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.