forked from bramus/ws1-sws-course-materials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.code.organization.html
416 lines (335 loc) · 11 KB
/
06.code.organization.html
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Webscripting1 — Serverside Webscripting — 06.code.organization</title>
<meta name="description" content="Webscripting1 — Serverside Webscripting — 06.code.organization">
<meta name="author" content="Bram(us) Van Damme - ikdoeict.be">
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css" media="screen">
<link rel="stylesheet" href="css/print.css" media="print">
<link rel="stylesheet" href="lib/zenburn.css">
<style>
.columns .column {
float: left;
list-style: none;
margin: 0 0 12px 0;
padding: 0;
}
.column-12 {
width: 50%;
text-align: center;
}
code {
color: gainsboro;
}
li > code, li em > code, li del > code, li ins > code, p > code {
background: #3F3F3F;
padding: 2px 4px;
box-shadow: 0px 0px 6px rgba(0,0,0,0.3);
font-size: 80%;
}
strong {
color: #553d00;
background: transparent url('assets/02/strong.png') no-repeat 50% 50%;
font-size: 80%;
padding: 0 4px;
font-family: palatino, times;
font-weight: 300;
font-style: italic;
}
#reveal section img.noborder {
background: transparent;
border: 0;
-webkit-box-shadow: none;
-moz-box-shadow: none;
-ms-box-shadow: none;
-o-box-shadow: none;
box-shadow: none;"
}
</style>
</head>
<body>
<div id="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- 0 : Title -->
<section>
<section>
<h3 class="inverted">Serverside Webscripting <small>[JLW322]</small></h3>
<h1>06.code<br />organization</h1>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
<script>
// Delicously hacky. Look away.
if( navigator.userAgent.match( /(iPhone|iPad|iPod|Android)/i ) )
document.write( '<p style="color: rgba(0,0,0,0.3); text-shadow: none;">('+'Tap to navigate'+')</p>' );
</script>
</section>
</section>
<section>
<section>
<h2>Include & Require</h2>
</section>
<section>
<h2>Include</h2>
<ul>
<li class="fragment">
Include the contents of one PHP file into another PHP file with the <code>include</code> keyword
<ul>
<li>
Contents <code>01_include.php</code>
<pre class="bigger"><code class="language-php"><?php
$x++;</code></pre>
</li>
<li>
Contents <code>01.php</code>
<pre class="bigger"><code class="language-php" data-overlay-example="assets/06/examples/01.php"><?php
$x = 0;
include '01_include.php';
include '01_include.php';
echo $x;</code></pre>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Include once</h2>
<ul>
<li class="fragment">
Include the contents of one PHP file <em>only once</em> into another PHP file with the <code>includ_once</code> keyword
<ul>
<li>
Contents <code>01b.php</code>
<pre class="bigger"><code class="language-php" data-overlay-example="assets/06/examples/01b.php"><?php
$x = 0;
include_once '01_include.php';
include_once '01_include.php';
echo $x;</code></pre>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Include #fail</h2>
<ul>
<li class="fragment">
If an included file does not exist, you'll get a notice
<ul>
<li>
Contents <code>01c.php</code>
<pre class="bigger"><code class="language-php" data-overlay-example="assets/06/examples/01c.php"><?php
$x = 0;
include '01_nonexistent.php';
include '01_include.php';
echo $x;</code></pre>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>require & require_once</h2>
<ul>
<li class="fragment">
<code>require</code> does the same as <code>include</code> yet it won't give a notice but will result in a fatal error and stop the process
<ul>
<li>
Contents <code>02.php</code>
<pre class="bigger"><code class="language-php" data-overlay-example="assets/06/examples/02.php"><?php
$x = 0;
require '02_nonexistent.php';
echo $x;</code></pre>
</li>
</ul>
</li>
<li class="fragment">I bet you can guess what <code>require_once</code> does</li>
<li class="fragment">
Best practice: always use <code>require</code> or <code>require_once</code> instead of <code>include*</code>
</li>
</ul>
</section>
</section>
<section>
<section>
<h2>DRY</h2>
</section>
<section>
<h2>DRY?</h2>
<ul>
<li class="fragment">
DRY = Don't Repeat Yourself
<ul>
<li>“<em>Every piece of knowledge must have a single, unambiguous, authoritative representation within a system</em>”</li>
</ul>
</li>
<li class="fragment">
If you have two files in your project which contain the same function/value you're not DRY, but WET <em>(Write Everything Twice)</em>
</li>
<li class="fragment">
One of the ways to remain DRY is to work with includes
</li>
</ul>
</section>
<section>
<h2>What needs to be DRY?</h2>
<ul>
<li class="fragment">
Projectwide variables
</li>
<li class="fragment">
Recurring functionality
</li>
</ul>
</section>
<section>
<h2>Projectwide variables</h2>
<ul>
<li class="fragment">
A variable that is constant and used throughout the project, yet is specific to the very project
<ul>
<li class="fragment">The mailserver to use when sending an e-mail</li>
<li class="fragment">The target mailaddress to send the various forms to</li>
<li class="fragment">The database configuration data (server, username, password, databasename)</li>
<li class="fragment">The API keys your website uses</li>
<li class="fragment">...</li>
</ul>
</li>
<li class="fragment">
You might also call this the configuration data
</li>
</ul>
</section>
<section>
<h2>How to?</h2>
<ul>
<li class="fragment">
Create a <code>config.php</code> file and <code>define</code> all variables into it
<pre class="bigger"><code class="language-php"><?php
define('MAIL_SERVER', 'localhost');
define('MAIL_PORT', 25);
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'Azerty123');
define('DB_NAME', 'world');
// EOF</code></pre>
<ul>
<li class="fragment">
Include this file everywhere, as the very first file to include and use the defined constants
</li>
</ul>
</li>
<li class="fragment">
<code>config.php</code> is the only file that may be changed when publishing a site onto a server
</li>
</ul>
</section>
<section>
<h2>Recurring Functionality</h2>
<ul>
<li class="fragment">
Say you have a function <code>isValidImageExtension()</code> that checks whether a filename ends in <code>.jpg</code>/<code>.jpeg</code>/<code>.gif</code>
<ul>
<li class="fragment">It'd be stupid to define this function in every file where you need it</li>
<li class="fragment">What if you want to check for <code>.png</code> too? You'll edit all files?</li>
</ul>
</li>
<li class="fragment">
A class definition — which itself is a step towards becoming DRY — only needs to be defined once
</li>
</ul>
</section>
<section>
<h2>How to?</h2>
<ul>
<li class="fragment">
Create a <code>functions.php</code> file and put all often used functions in it <em>(*)</em>
<ul>
<li class="fragment">
Include this file everywhere, as the very first file to include and use the defined constants
</li>
</ul>
</li>
<li class="fragment">
If you define a class, define it in a separate file on disk <em>(*)</em>
<ul>
<li>Include the file when necessary</li>
</ul>
</li>
</ul>
<footer class="fragment">(*) Until we start using libraries and talk about PSR-0 this way of organizing is sufficient</footer>
</section>
</section>
<section>
<section>
<h2>On Disk / Summary</h2>
</section>
<section>
<h2>Code Organization on disk</h2>
<ul>
<li class="fragment">
Make use of an <code>includes/</code> subfolder to store <code>config.php</code> and <code>functions.php</code> in
<ul class="fragment">
<li>Define your configuration data in <code>config.php</code></li>
<li>Place commonly used functions in <code>functions.php</code> <em>(*)</em></li>
<li>Always include these two files in your project</li>
</ul>
</li>
<li class="fragment">
Store all classes into <code>includes/classes/</code> <em>(*)</em>
<ul class="fragment">
<li>Include when necessary</li>
</ul>
</li>
</ul>
<footer class="fragment">(*) Again: Until we start using libraries and talk about PSR-0 this way of organizing is sufficient</footer>
</section>
</section>
<!-- The END -->
<section>
<section>
<h2>Questions?</h2>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
</section>
</section>
<!-- Sources -->
<section id="sources">
<section>
<h2>Sources</h2>
<ul>
</ul>
</section>
</section>
</div>
<!-- The navigational controls UI -->
<aside class="controls">
<a class="left" href="#">◄</a>
<a class="right" href="#">►</a>
<a class="up" href="#">▲</a>
<a class="down" href="#">▼</a>
<span id="revealIndex">/</span>
</aside>
<!-- Index Link -->
<aside class="back">
<a href="index.html">← Back to index</a>
</aside>
<!-- ikdoeict.be Link -->
<a href="http://www.ikdoeict.be/" title="ikdoeict.be" id="ikdoeict">ikdoeict.be</a>
<!-- Displays presentation progress, max value changes via JS to reflect # of slides -->
<div class="progress"><span></span></div>
</div>
<script src="js/reveal.js"></script>
<script src="lib/highlight.js"></script>
<script src="lib/prefixfree.js"></script>
<script src="lib/css-snippets.js"></script>
<script src="lib/css-edit.js"></script>
<script src="lib/incrementable.js"></script>
<script src="js/main.js"></script>
</body>
</html>