1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Naming Conventions - Zend Framework Manual</title>
</head>
<body>
<table width="100%">
<tr valign="top">
<td width="85%">
<table width="100%">
<tr>
<td width="25%" style="text-align: left;">
<a href="coding-standard.php-file-formatting.html">PHP File Formatting</a>
</td>
<td width="50%" style="text-align: center;">
<div class="up"><span class="up"><a href="coding-standard.html">Zend Framework Coding Standard for PHP</a></span><br />
<span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
</td>
<td width="25%" style="text-align: right;">
<div class="next" style="text-align: right; float: right;"><a href="coding-standard.coding-style.html">Coding Style</a></div>
</td>
</tr>
</table>
<hr />
<div id="coding-standard.naming-conventions" class="section"><div class="info"><h1 class="title">Naming Conventions</h1></div>
<div class="section" id="coding-standard.naming-conventions.classes" name="coding-standard.naming-conventions.classes"><div class="info"><h1 class="title">Classes</h1></div>
<p class="para">
Zend Framework standardizes on a class naming convention whereby the names of the
classes directly map to the directories in which they are stored. The root level
directory of Zend Framework's standard library is the "Zend/" directory, whereas
the root level directory of Zend Framework's extras library is the "ZendX/"
directory. All Zend Framework classes are stored hierarchically under these root
directories..
</p>
<p class="para">
Class names may only contain alphanumeric characters. Numbers are permitted
in class names but are discouraged in most cases. Underscores are only permitted in
place of the path separator; the filename "<var class="filename">Zend/Db/Table.php</var>"
must map to the class name "<span class="classname">Zend_Db_Table</span>".
</p>
<p class="para">
If a class name is comprised of more than one word, the first letter of each new
word must be capitalized. Successive capitalized letters are not allowed, e.g.
a class "Zend_PDF" is not allowed while "<span class="classname">Zend_Pdf</span>" is
acceptable.
</p>
<p class="para">
These conventions define a pseudo-namespace mechanism for Zend Framework. Zend
Framework will adopt the <acronym class="acronym">PHP</acronym> namespace feature when it becomes
available and is feasible for our developers to use in their applications.
</p>
<p class="para">
See the class names in the standard and extras libraries for examples of this
classname convention.
</p>
<blockquote><p><b class="note">Note</b>:
<em class="emphasis">Important</em>: Code that must be deployed alongside
Zend Framework libraries but is not part of the standard or extras libraries
(e.g. application code or libraries that are not distributed by Zend) must
never start with "Zend_" or "ZendX_".
<br />
</p></blockquote>
</div>
<div class="section" id="coding-standard.naming-conventions.abstracts" name="coding-standard.naming-conventions.abstracts"><div class="info"><h1 class="title">Abstract Classes</h1></div>
<p class="para">
In general, abstract classes follow the same conventions as <a href="coding-standard.naming-conventions.html#coding-standard.naming-conventions.classes" class="link">classes</a>,
with one additional rule: abstract class names must end in the term, "Abstract",
and that term must not be preceded by an underscore. As an example,
<span class="classname">Zend_Controller_Plugin_Abstract</span> is considered an
invalid name, but <span class="classname">Zend_Controller_PluginAbstract</span> or
<span class="classname">Zend_Controller_Plugin_PluginAbstract</span> would be valid
names.
</p>
<blockquote><p><b class="note">Note</b>:
This naming convention is new with version 1.9.0 of Zend Framework. Classes
that pre-date that version may not follow this rule, but will be renamed in
the future in order to comply.
<br />
The rationale for the change is due to namespace usage. As we look towards Zend
Framework 2.0 and usage of <acronym class="acronym">PHP</acronym> 5.3, we will be using
namespaces. The easiest way to automate conversion to namespaces is to simply
convert underscores to the namespace separator -- but under the old naming
conventions, this leaves the classname as simply "Abstract" or "Interface" --
both of which are reserved keywords in <acronym class="acronym">PHP</acronym>. If we prepend the
(sub)component name to the classname, we can avoid these issues.
<br />
To illustrate the situation, consider converting the class
<span class="classname">Zend_Controller_Request_Abstract</span> to use namespaces:
<br />
<div class="programlisting php"><div class="phpcode"><div class="php" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">namespace Zend\Controller\Request;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">abstract <span style="color: #000000; font-weight: bold;">class</span> Abstract</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">{</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #808080; font-style: italic;">// ...</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">}</span></div></li></ol></div></div></div>
Clearly, this will not work. Under the new naming conventions, however, this
would become:
<br />
<div class="programlisting php"><div class="phpcode"><div class="php" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">namespace Zend\Controller\Request;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">abstract <span style="color: #000000; font-weight: bold;">class</span> RequestAbstract</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">{</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #808080; font-style: italic;">// ...</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">}</span></div></li></ol></div></div></div>
We still retain the semantics and namespace separation, while omitting the
keyword issues; simultaneously, it better describes the abstract class.
<br />
</p></blockquote>
</div>
<div class="section" id="coding-standard.naming-conventions.interfaces" name="coding-standard.naming-conventions.interfaces"><div class="info"><h1 class="title">Interfaces</h1></div>
<p class="para">
In general, interfaces follow the same conventions as <a href="coding-standard.naming-conventions.html#coding-standard.naming-conventions.classes" class="link">classes</a>,
with one additional rule: interface names may optionally end in the term,
"Interface", but that term must not be preceded by an underscore. As an example,
<span class="classname">Zend_Controller_Plugin_Interface</span> is considered an
invalid name, but <span class="classname">Zend_Controller_PluginInterface</span> or
<span class="classname">Zend_Controller_Plugin_PluginInterface</span> would be valid
names.
</p>
<p class="para">
While this rule is not required, it is strongly recommended, as it provides a
good visual cue to developers as to which files contain interfaces rather than
classes.
</p>
<blockquote><p><b class="note">Note</b>:
This naming convention is new with version 1.9.0 of Zend Framework. Classes
that pre-date that version may not follow this rule, but will be renamed in
the future in order to comply. See <a href="coding-standard.naming-conventions.html#coding-standard.naming-conventions.abstracts" class="link">the previous
section</a> for more information on the rationale for this change.
<br />
</p></blockquote>
</div>
<div class="section" id="coding-standard.naming-conventions.filenames" name="coding-standard.naming-conventions.filenames"><div class="info"><h1 class="title">Filenames</h1></div>
<p class="para">
For all other files, only alphanumeric characters, underscores, and the dash
character ("-") are permitted. Spaces are strictly prohibited.
</p>
<p class="para">
Any file that contains <acronym class="acronym">PHP</acronym> code should end with the extension
"<var class="filename">.php</var>", with the notable exception of view scripts. The
following examples show acceptable filenames for Zend Framework classes:
</p>
<div class="programlisting php"><div class="phpcode"><div class="php" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Zend/Db.php</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Zend/Controller/Front.php</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Zend/View/Helper/FormRadio.php</div></li></ol></div></div></div>
<p class="para">
File names must map to class names as described above.
</p>
</div>
<div class="section" id="coding-standard.naming-conventions.functions-and-methods" name="coding-standard.naming-conventions.functions-and-methods"><div class="info"><h1 class="title">Functions and Methods</h1></div>
<p class="para">
Function names may only contain alphanumeric characters. Underscores are not
permitted. Numbers are permitted in function names but are discouraged in most
cases.
</p>
<p class="para">
Function names must always start with a lowercase letter. When a function name
consists of more than one word, the first letter of each new word must be
capitalized. This is commonly called "camelCase" formatting.
</p>
<p class="para">
Verbosity is generally encouraged. Function names should be as verbose as is
practical to fully describe their purpose and behavior.
</p>
<p class="para">
These are examples of acceptable names for functions:
</p>
<div class="programlisting php"><div class="phpcode"><div class="php" style="font-family: monospace;"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">filterInput<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">getElementById<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">widgetFactory<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span></div></li></ol></div></div></div>
<p class="para">
For object-oriented programming, accessors for instance or static variables should
always be prefixed with "get" or "set". In implementing design patterns, such as the
singleton or factory patterns, the name of the method should contain the pattern
name where practical to more thoroughly describe behavior.
</p>
<p class="para">
For methods on objects that are declared with the "private" or "protected" modifier,
the first character of the method name must be an underscore. This is the only
acceptable application of an underscore in a method name. Methods declared "public"
should never contain an underscore.
</p>
<p class="para">
Functions in the global scope (a.k.a "floating functions") are permitted but
discouraged in most cases. Consider wrapping these functions in a static class.
</p>
</div>
<div class="section" id="coding-standard.naming-conventions.variables" name="coding-standard.naming-conventions.variables"><div class="info"><h1 class="title">Variables</h1></div>
<p class="para">
Variable names may only contain alphanumeric characters. Underscores are not
permitted. Numbers are permitted in variable names but are discouraged in most
cases.
</p>
<p class="para">
For instance variables that are declared with the "private" or "protected" modifier,
the first character of the variable name must be a single underscore. This is the
only acceptable application of an underscore in a variable name. Member variables
declared "public" should never start with an underscore.
</p>
<p class="para">
As with function names (see section 3.3) variable names must always start with a
lowercase letter and follow the "camelCaps" capitalization convention.
</p>
<p class="para">
Verbosity is generally encouraged. Variables should always be as verbose as
practical to describe the data that the developer intends to store in them. Terse
variable names such as "<var class="varname">$i</var>" and "<var class="varname">$n</var>" are
discouraged for all but the smallest loop contexts. If a loop contains more than
20 lines of code, the index variables should have more descriptive names.
</p>
</div>
<div class="section" id="coding-standard.naming-conventions.constants" name="coding-standard.naming-conventions.constants"><div class="info"><h1 class="title">Constants</h1></div>
<p class="para">
Constants may contain both alphanumeric characters and underscores. Numbers are
permitted in constant names.
</p>
<p class="para">
All letters used in a constant name must be capitalized, while all words in a
constant name must be separated by underscore characters.
</p>
<p class="para">
For example, <b><tt>EMBED_SUPPRESS_EMBED_EXCEPTION</tt></b> is permitted but
<b><tt>EMBED_SUPPRESSEMBEDEXCEPTION</tt></b> is not.
</p>
<p class="para">
Constants must be defined as class members with the "const" modifier. Defining
constants in the global scope with the "define" function is permitted but strongly
discouraged.
</p>
</div>
</div>
<hr />
<table width="100%">
<tr>
<td width="25%" style="text-align: left;">
<a href="coding-standard.php-file-formatting.html">PHP File Formatting</a>
</td>
<td width="50%" style="text-align: center;">
<div class="up"><span class="up"><a href="coding-standard.html">Zend Framework Coding Standard for PHP</a></span><br />
<span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
</td>
<td width="25%" style="text-align: right;">
<div class="next" style="text-align: right; float: right;"><a href="coding-standard.coding-style.html">Coding Style</a></div>
</td>
</tr>
</table>
</td>
<td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
float: left;
width: 186px;
padding: 5px;
font-size: smaller;
}
ul.toc {
margin: 0px 5px 5px 5px;
padding: 0px;
}
ul.toc li {
font-size: 85%;
margin: 1px 0 1px 1px;
padding: 1px 0 1px 11px;
list-style-type: none;
background-repeat: no-repeat;
background-position: center left;
}
ul.toc li.header {
font-size: 115%;
padding: 5px 0px 5px 11px;
border-bottom: 1px solid #cccccc;
margin-bottom: 5px;
}
ul.toc li.active {
font-weight: bold;
}
ul.toc li a {
text-decoration: none;
}
ul.toc li a:hover {
text-decoration: underline;
}
</style>
<ul class="toc">
<li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
<li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
<li class="header up"><a href="coding-standard.html">Zend Framework Coding Standard for PHP</a></li>
<li><a href="coding-standard.overview.html">Overview</a></li>
<li><a href="coding-standard.php-file-formatting.html">PHP File Formatting</a></li>
<li class="active"><a href="coding-standard.naming-conventions.html">Naming Conventions</a></li>
<li><a href="coding-standard.coding-style.html">Coding Style</a></li>
</ul>
</td>
</tr>
</table>
</body>
</html>
|