-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1413 lines (1334 loc) · 43.8 KB
/
index.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP 7: The New New PHP</title>
<meta name="author" content="Larry Garfield">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="platform-reveal/platform-theme/assets/css/styles.css" id="theme">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'platform-theme/assets/css/print/pdf.css' : 'platform-theme/assets/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<script src="platform-reveal/countup.js"></script>
<script src="platform-reveal/classer.js"></script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<style type="text/css">
.reveal img.graphic {
width: 100%;
border-radius: 5px;
}
img.screenshot {
border-radius: 5px;
width: 800px;
}
ul.inline {
margin: 0;
padding: 0;
list-style-type: none;
text-align: center;
}
ul.inline li {
display: inline;
}
p.bigdeal {
font-size: 200%;
color: Red;
font-style: italic;
text-shadow: 3px 3px 10px #990000;
}
.reveal pre + pre,
.reveal p + pre,
.reveal pre + p,
.reveal p + p {
margin-top: 1em;
}
ul.project-list {
display: flex;
flex-wrap: wrap;
list-style: none;
justify-content: space-around;
align-items: baseline;
}
ul.project-list > li {
padding: 20px;
}
ul.project-list > li > img {
width: 150px;
}
p.fullscreen-credit {
margin-top: 90%;
font-size: small;
}
</style>
</head>
<!-- Change body class to "dark" if you prefer a dark theme, or "light" if you'd rather have a light theme-->
<body class="light">
<div class="reveal">
<div class="logo">
<p>Platform.sh</p>
</div>
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>PHP 7<span class="fragment">[.2]</span></h1>
<h2>The New New PHP</h2>
<p>
<small>Presented by <a href="http://www.garfieldtech.com/">Larry Garfield</a> (<a href="http://twitter.com/Crell">@Crell</a>)</small>
</p>
</section>
<section class="l-2up">
<h2><a href="http://twitter.com/Crell">@Crell</a></h2>
<div>
<img src="assets/larry-olaf-hug.jpg" alt="Larry implements Huggable" style="width: 350px; margin: 0" />
</div>
<div>
<ul style="text-wrap: none">
<li>Director of DX, <a href="http://www.platform.sh/">Platform.sh</a></li>
<li style="margin-top: 1em;"><a href="http://www.php-fig.org/">PHP-FIG</a> Core Committee</li>
<li style="margin-top: 1em;"><code>implements Huggable</code></li>
</ul>
</div>
</section>
<section>
<section>
<img src="assets/php7-logo.svg" alt="PHP 7" data-credit="http://www.cowburn.info/2015/06/18/php7-logo/" />
<aside class="notes">
<ul>
<li>PHP 6 snuck into PHP 5 party dressed as 5.3, hasn't been seen since</li>
</ul>
</aside>
</section>
<section>
<h3>Show me the data!</h3>
<img src="assets/composer-2018-01.png" alt="Most developers are now on PHP 7" data-credit="https://seld.be/notes/php-versions-stats-2018-1-edition" />
<p><small>(Credit: <a href="https://seld.be/notes/php-versions-stats-2018-1-edition">https://seld.be/notes/php-versions-stats-2018-1-edition</a>)</small></p>
<aside class="notes">
<ul>
<li>What users have when running composer.</li>
</ul>
</aside>
</section>
<section>
<h3>You're missing</h3>
<img src="assets/composer-2018.1-requirements.png" width="80%" alt="But most packages target PHP 5." data-credit="https://seld.be/notes/php-versions-stats-2018-1-edition" />
<p><small>(Credit: <a href="https://seld.be/notes/php-versions-stats-2018-1-edition">https://seld.be/notes/php-versions-stats-2018-1-edition</a>)</small></p>
<aside class="notes">
<ul>
<li>What packages depend on</li>
<li>You're lagging! This is for you.</li>
</ul>
</aside>
</section>
</section>
<section>
<section>
<!-- return types-->
<h1>#1</h1>
</section>
<section>
<p class="bigtext"><q>Do you trust your docblock?</q></p>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
class User {
protected $id;
protected $address;
public function getAddress() {
return $this->address;
}
}
function compute_shipping($src, User $user) {
$dest = $user->getAddress();
$distance = distance_between($src, $dest);
return $distance * .5; // 50 cents/mile
}
</code></pre>
<aside class="notes">
<ul>
<li>What does getAddress() return? String? Address object?</li>
<li>Does compute_shipping() want the same thing?</li>
</ul>
</aside>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
class User {
protected $id;
protected $address;
/**
* @return Address
*/
public function getAddress() {
return $this->address;
}
}
function compute_shipping($src, User $user) {
$dest = $user->getAddress();
$distance = distance_between($src, $dest);
return $distance * .5; // 50 cents/mile
}
</code></pre>
<aside class="notes">
<ul>
<li>We claim it's an Address, but is it?</li>
<li>Could always be null.</li>
</ul>
</aside>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
class User {
protected $id;
protected $address;
public function getAddress() : Address {
return $this->address;
}
}
function compute_shipping($src, User $user) {
$dest = $user->getAddress();
$distance = distance_between($src, $dest);
return $distance * .5; // 50 cents/mile
}
</code></pre>
<aside class="notes">
<ul>
<li>Always and ever an Address!</li>
</ul>
</aside>
</section>
<section>
<h2>Return types</h2>
<ul>
<li class="fragment">New in PHP 7!</li>
<li class="fragment">Not nullable until 7.1</li>
</ul>
<aside class="notes">
<ul>
<li>Not null avoids errors</li>
</ul>
</aside>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
$r = new UserRepository();
print $r->findById(123)->getAddress()->getStreet() . PHP_EOL;
</code></pre>
<pre class="fragment font--24"><code class="text" data-trim>
Method called on non-object
</code></pre>
<p style="margin-top: 1em" class="fragment">No NULL means:</p>
<ul>
<li class="fragment">Exception</li>
<li class="fragment">"Empty" value object</li>
</ul>
<aside class="notes">
<ul>
<li>Now you sigh and add a dozen if statements.</li>
<li>If typed returns, can't be null! Blows up sooner.</li>
<li>Solution depends on use case.</li>
</ul>
</aside>
</section>
<section>
<h2>For Repositories etc.</h2>
<pre class="font--24"><code class="php" data-trim>
class UserRepository {
// ...
public function findById($id) : User {
if (/* can't find the user */) {
throw new InvalidArgumentException('No such User: ' . $id);
}
return $user;
}
}
</code></pre>
<aside class="notes">
<ul>
<li>The rest of the code likely won't work anyway.</li>
</ul>
</aside>
</section>
<section>
<h2>For Value objects</h2>
<pre class="font--24"><code class="php" data-trim>
interface AddressInterface {
public function getStreet();
public function getCity();
public function getState();
public function getZip();
}
class EmptyAddress implements AddressInterface {
public function getStreet() { return ''; }
public function getCity() { return ''; }
public function getState() { return ''; }
public function getZip() { return ''; }
}
class Address implements AddressInterface {
// ...
}
</code></pre>
<aside class="notes">
<ul>
<li>An empty value is useful, sometimes.</li>
<li>Very case-by-case.</li>
<li>Still better than NULL</li>
</ul>
</aside>
</section>
<section>
<p class="bigtext"><a href="http://www.garfieldtech.com/blog/empty-return-values">Returning false on error</a> is a<br />big 'screw you' to your users</p>
<aside class="notes">
<ul>
<li>Undermines type expectations</li>
<li>Forces callers to litter code with if-checks.</li>
<li>User can't rely on types to avoid errors</li>
</ul>
</aside>
</section>
<section>
<h2>But in 7.1...</h2>
<pre class="font--24"><code class="php" data-trim>
class UserRepository {
// ...
public function findById($id) : ?User {
if (/* can't find the user */) {
return null;
}
return $user;
}
}
</code></pre>
<p class="fragment">(Please don't)</p>
</section>
<section>
<h2><code>void</code> returns</h2>
<pre class="font--24"><code class="php" data-trim>
function return_to_sender() : void {
// Do stuff
// ...
return; // Legal
return null; // Not OK
return 1; // Not even a little OK
}
</code></pre>
</section>
</section>
<section>
<!-- scalar types -->
<section>
<h1>#2</h1>
</section>
<section data-transition="slide-in fade-out">
<pre class="font--20"><code class="php" data-trim>
function compute_shipping($src, AddressInterface $dest) {
$distance = distance_between($src, $dest);
return $distance * .5; // 50 cents/mile
}
function distance_between(AddressInterface $src, AddressInterface $dest) {
// ...
}
$u_address = new Address(); // ...
$user = new User(123, $u_address);
$src = new Address(); // ...
compute_shipping($src, $user->getAddress());
</code></pre>
<p class="fragment">What does distance_between() return?</p>
<aside class="notes">
<ul>
<li>Could have made that change before, but more obvious when typed</li>
<li>distance_between() seems to return number. int or float?</li>
</ul>
</aside>
</section>
<section data-transition="fade-in slide-out">
<pre class="font--18"><code class="php" data-trim>
function compute_shipping(AddressInterface $src, AddressInterface $dest) : float {
$distance = distance_between($src, $dest);
return $distance * .5; // 50 cents/mile
}
function distance_between(AddressInterface $src, AddressInterface $dest) : int {
// ...
}
$u_address = new Address(); // ...
$user = new User(123, $u_address);
$src = new Address(); // ...
compute_shipping($src, $user->getAddress());
</code></pre>
<aside class="notes">
<ul>
<li>Assume whole miles. Round up, because greedy.</li>
</ul>
</aside>
</section>
<section>
<p class="bigtext"><q>Everyone here owes<br />Andrea Faulds and Anthony Ferrara a drink</q></p>
</section>
<section>
<h2>New type specs in PHP 7</h2>
<ul class="fragment">
<li>int</li>
<li>float</li>
<li>string</li>
<li>bool</li>
</ul>
<aside class="notes">
<ul>
<li>No integer/boolean</li>
</ul>
</aside>
</section>
<section>
<h2>Full type list</h2>
<ul>
<li>int</li>
<li>float</li>
<li>string</li>
<li>bool</li>
<li>array</li>
<li>callable</li>
<li>object</li>
<li>Any class name</li>
</ul>
<aside class="notes">
<ul>
<li>All usable as parameters or returns</li>
<li>object new in PHP 7.2</li>
</ul>
</aside>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
interface AddressInterface {
public function getStreet();
public function getCity();
public function getState();
public function getZip();
}
</code></pre>
<p class="fragment">Is getStreet() a string or object?</p>
<aside class="notes">
<ul>
<li>Object with number, name, apartment?</li>
</ul>
</aside>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
interface AddressInterface {
public function getStreet() : string;
public function getCity() : string;
public function getState() : string;
public function getZip() : string;
}
</code></pre>
<pre class="fragment font--18"><code class="php" data-trim>
class Address implements AddressInterface {
public function __construct(string $street, string $city, string $state, string $zip) {
// ...
}
// ...
}
</code></pre>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
$u_address = new Address('45 Hull St', 'Boston', 'MA', 02215);
$user = new User(123, $u_address);
$src = new Address('1060 W Addison St', 'Chicago', 'IL', 60613);
compute_shipping($src, $user->getAddress());
</code></pre>
<aside class="notes">
<ul>
<li>Point out error. Becomes 1165 in Octal</li>
<li>Biggest dispute in PHP scalar typing</li>
</ul>
</aside>
</section>
<section>
<h2>Strict vs Weak mode</h2>
<p class="fragment">Weak by default</p>
<p class="fragment">* string -> int may E_NOTICE</p>
<aside class="notes">
<ul>
<li>Casts like we're used to</li>
<li>PHP used to auto-coercing, because web</li>
<li>Unsafe</li>
<li>String -> int notice if non-numeric.</li>
<li>Undermines type checking</li>
</ul>
</aside>
</section>
<section>
<h2>Strict mode</h2>
<p>Address.php</p>
<pre class="font--20"><code class="php" data-trim>
declare(strict_types=1);
class Address implements AddressInterface {
public function __construct(string $street, string $city, string $state, string $zip) {
// ...
}
public function getZip() : string {
return $this->zip;
}
// ...
}
</code></pre>
<p class="fragment">Affects calls and returns <em>from</em> this file</p>
<aside class="notes">
<ul>
<li>If $this->zip not a string, TypeError thrown</li>
<li>Tooling can spot it!</li>
<li>Does not affect constructor!</li>
</ul>
</aside>
</section>
<section>
<h2>Strict mode</h2>
<p>setup.php</p>
<pre class="font--24"><code class="php" data-trim>
declare(strict_types=1);
$u_address = new Address('4 Yawkey Way', 'Boston', 'MA', 02215);
$user = new User(123, $u_address);
$src = new Address('1060 W Addison St', 'Chicago', 'IL', 60613);
compute_shipping($src, $user->getAddress());
</code></pre>
<p class="fragment">Affects calls and returns <em>from</em> this file</p>
<aside class="notes">
<ul>
<li>Will throw TypeError</li>
</ul>
</aside>
</section>
<section>
<h2>Casting in strict mode</h2>
<p>* int converts to float automatically</p>
<aside class="notes">
<ul>
<li>Otherwise, (cast) or redesign</li>
<li>Really, redesign</li>
</ul>
</aside>
</section>
<section>
<h2>Using strict types</h2>
<p class="fragment bigtext">90% of the time</p>
<p class="fragment bigtext">(Everywhere except Input)</p>
<aside class="notes">
<ul>
<li>Catches the most bugs</li>
<li>Pass data FROM IO to a typed function, weak, coerce</li>
<li>Get the most type safety</li>
<li>Drives good design; separate methods, predictable returns, etc.</li>
</ul>
</aside>
</section>
<section>
<p class="bigtext">Also, Documentation</p>
<aside class="notes">
<ul>
<li>"Good code is self-documenting"</li>
<li>Static type or you're a hypocrite</li>
<li>Static types can't go out of date like docblocs</li>
</ul>
</aside>
</section>
<section>
<h2>BC Warning!</h2>
<img src="assets/bc-caveman.jpg" align="BC Warning" data-credit="https://www.flickr.com/photos/alfer/1349385195" />
<p class="bigtext">New reserved words!</p>
</section>
</section>
<section>
<!-- Spaceship -->
<section>
<h1>#3</h1>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
usort($array, function ($a, $b) {
if ($a < $b) {
return -1;
}
elseif ($a > $b) {
return 1;
}
else {
return 0;
}
});
</code></pre>
</section>
<section>
<h1>T_SPACESHIP</h1>
<h1 style="margin-top: 1em;"><=></h1>
</section>
<section data-background="assets/tie-interceptor.jpg" data-background-size="100%" data-credit="http://fractalsponge.net/?p=128">
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
usort ($array, function($a, $b) {
return $a <=> $b;
});
</code></pre>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
usort($people, function (Person $a, Person $b) {
if ($a->lastName() < $b->lastName()) {
return -1;
}
elseif ($a->lastName() > $b->lastName()) {
return 1;
}
else {
if ($a->firstName() < $b->firstName()) {
return -1;
}
elseif ($a->firstName() > $b->firstName()) {
return 1;
}
else {
return 0;
}
}
});
</code></pre>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
usort($people, function (Person $a, Person $b) {
return $a->lastName() < $b->lastName() ? -1
: ($a->lastName() > $b->lastName() ? 1
: $a->firstName() < $b->firstName() ? -1
: ($a->firstName() > $b->firstName() ? 1 : 0));
});
</code></pre>
<p id="fired" class="fragment" data-classer="fired" data-classer-class="bigdeal" style="margin-top: 1em;">You're Fired!</p>
</section>
<section>
<pre class="font--20"><code class="php" data-trim>
usort($people, function (Person $a, Person $b) {
return [$a->lastName(), $a->firstName()] <=> [$b->lastName(), $b->firstName()];
});
</code></pre>
</section>
</section>
<section>
<!-- Null coalesce -->
<section>
<h1>#4</h1>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
$username = $username ? $username : 'Anonymous';
</code></pre>
<pre class="fragment font--24" style="margin-top: 1em;"><code class="php" data-trim>
$username = $username ?: 'Anonymous';
</code></pre>
<p class="fragment bigtext">But what if $username doesn't exist?</p>
<pre class="fragment font--24" style="margin-top: 1em;"><code class="text" data-trim>
Notice: Undefined variable: username on line 3
</code></pre>
</section>
<section>
<pre class="font--20"><code class="php" data-trim>
$username = isset($username) && !is_null($username) ? $username : 'Anonymous';
</code></pre>
<p class="fragment">NULL coalesce!</p>
<pre class="fragment font--24"><code class="php" data-trim>
$username = $username ?? 'Anonymous';
</code></pre>
<pre class="fragment font--20"><code class="php" data-trim>
$username = $submitted['username'] ?? $user->username() ?? 'Anonymous';
</code></pre>
<p class="fragment bigtext">?? checks setness, not truthiness</p>
</section>
</section>
<section>
<!-- CSPRING -->
<section>
<h1>#5</h1>
</section>
<section>
<p class="bigtext">Cryptography is hard</p>
<aside class="notes">
<ul>
<li>Need big random numbers</li>
<li>Random isn't random in computers</li>
<li>"Sufficiently" random</li>
</ul>
</aside>
</section>
<section>
<h2>Don't we have this already?</h2>
<ul>
<li class="fragment"><code>rand()</code> isn't really random</li>
<li class="fragment"><code>mcrypt()</code> isn't really random (also unsupported)</li>
<li class="fragment"><code>fread(fopen('/dev/urandom'), 16)</code> isn't portable</li>
<li class="fragment">openssl is hard to use</li>
</ul>
</section>
<section>
<img src="assets/picard-riker-facepalm.jpg" alt="Picard-Riker Face Palm" />
</section>
<section>
<p class="bigtext">What we want is an easy <br /><b>C</b>ryptographically <b>S</b>ecure <b>P</b>seudo-<b>R</b>andom <b>N</b>umber <b>G</b>enerator</p>
<p class="bigtext fragment">CSPRNG!</p>
</section>
<section>
<p class="bigtext">Ask and ye shall receive…</p>
<pre class="fragment font--24"><code class="php" data-trim>
$junk = random_bytes(16);
$val = random_int(1, 100);
</code></pre>
<div class="fragment" style="margin-top: 1em;">
<h3>On PHP 5?</h3>
<p><code>composer require paragonie/random_compat</code></p>
<a href="https://github.com/paragonie/random_compat">https://github.com/paragonie/random_compat</a>
</div>
<aside class="notes">
<ul>
<li>Bytes, not chars; do not print</li>
<li>random_int() is inclusive.</li>
<li>Throws exceptions if can't run.</li>
</ul>
</aside>
</section>
<section>
<p class="bigtext">If you're not using these functions for security...</p>
<ul class="fragment">
<li>password_hash() / password_verify()</li>
<li>random_bytes()</li>
<li>random_int()</li>
</ul>
<p id="security-wrong" class="fragment" data-classer="security-wrong" data-classer-class="bigdeal" style="margin-top: 1em;">You're Doing It Wrong!</p>
</section>
<section class="l-2up">
<h2>BC Warning!</h2>
<div>
<p>Reserved function names</p>
<ul>
<li><code>random_bytes()</code></li>
<li><code>random_int()</code></li>
</ul>
</div>
<div><img src="assets/bc-mammoth.jpg" alt="BC Warning" data-credit="https://www.flickr.com/photos/sabeth718/3621132155" /></div>
</section>
</section>
<section>
<!-- Anonymous classes -->
<section>
<h1>#6</h1>
</section>
<section>
<h2>Mocking is hard</h2>
<pre class="font--24"><code class="php" data-trim>
$mock_repository = $this
->getMockBuilder(ThingieRepository::class)
->disableOriginalConstructor()
->getMock();
$obj1 = new Thingie();
$obj1->setVal('abc');
$obj2 = $this->getMockBuilder(Thingie::class)
->disableOriginalConstructor()
->getMock();
$obj1 ->method('getVal')->willReturn('def');
$map = [ [1, $obj1], [2, $obj2] ];
$mock_repository
->method('load')->will($this->returnValueMap($map));
$subject = new ClassUnderTest($mock_repository);
$this->assertTrue($subject->findThings());
</code></pre>
<aside class="notes">
<ul>
<li>Ugly mock DSL</li>
<li>Dates from PHP 5.2 days or earlier</li>
<li>What we really want is a custom class, but put the code where?</li>
</ul>
</aside>
</section>
<section>
<h2>Fakes are easy</h2>
<pre class="font--24"><code class="php" data-trim>
$fake_repository = new class extends ThingieRepository {
public function __construct() {}
public function load($id) {
switch ($id) {
case 1:
$obj1 = new Thingie();
$obj1->setVal('abc');
return $obj1;
case 2:
return new class extends Thingie {
public function getVal() {
return 'def';
}
};
}
}
};
$subject = new ClassUnderTest($fake_repository);
$this->assertTrue($subject->findThings());
</code></pre>
<aside class="notes">
<ul>
<li>Single-use class, define on instantiation</li>
<li>Mostly useful for testing</li>
<li>Can extend, implement, traits, pass type checks, NOT serialize</li>
</ul>
</aside>
</section>
<section>
<h2>One-off dependencies</h2>
<pre class="font--24"><code class="php" data-trim>
new Service(new class implements LoggerInterface {
use LoggerTrait;
public function log($message) {
print $message . PHP_EOL;
}
});
</code></pre>
<aside class="notes">
<ul>
<li>Also useful for debugging.</li>
</ul>
</aside>
</section>
<section>
<h2>Constructors, too</h2>
<pre class="font--24"><code class="php" data-trim>
new class($logger) implements ServiceInterface {
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function doServiceStuff() { }
};
</code></pre>
<aside class="notes">
<ul>
<li>As with anon functions, don't get too carried away.</li>
</ul>
</aside>
</section>
</section>
<section>
<!-- Engine exceptions / Errors -->
<section>
<h1>#7</h1>
</section>
<section>
<p class="bigtext">Fatal errors suck</p>
<p class="fragment bigtext">Recoverable errors… aren't</p>
<aside class="notes">
<ul>
<li>No cleanup</li>
<li>No finally, no destructors</li>
<li>No context in error handler</li>
</ul>
</aside>
</section>
<section>
<h2>PHP 5</h2>
<pre class="font--24"><code class="php" data-trim>
function doStuff(Request $r) {
// ...
}
$u = new User();
doStuff($u);
</code></pre>
<p class="fragment"><code>Catchable fatal error: Argument 1 passed to doStuff() must be an instance of Request,<br />instance of User given</code></p>
<aside class="notes">
<ul>
<li>Why catchable if I can't catch?</li>
</ul>
</aside>
</section>
<section>
<h2>PHP 7</h2>
<pre class="font--24"><code class="php" data-trim>
function doStuff(Request $r) {
// ...
}
$u = new User();
try {
doStuff($u);
}
catch (\TypeError $e) {
print "Wrong variable type, dummy." . PHP_EOL;
}
</code></pre>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
try {
include 'buggy_file.php';
}
catch (\ParseError $e) {
$logger->error("That file is buggy!");
}
</code></pre>
</section>
<section>
<pre class="font--24"><code class="php" data-trim>
try {
nonexistant_function();
}
catch (\Error $e) {
print "The error was " . $e->getMessage();
}
</code></pre>
</section>
<section>
<div><img src="assets/throwable-hierarchy.svg" alt="Throwable is at the root of all Errors and Exceptions" data-credit="Larry Garfield" /></div>
<p class="fragment" style="text-decoration: line-through">E_FATAL, E_RECOVERABLE_ERROR, E_PARSE, E_COMPILE_ERROR</p>
</section>
<section>
<p class="bigtext">Exceptions: The user screwed up</p>
<p class="bigtext">Errors: The coder screwed up</p>
<aside class="notes">
<ul>
<li>Really only useful for high-level log/cleanup</li>
<li>Unlikely you can continue anyway.</li>
</ul>
</aside>
</section>
<section>
<h2>BC Warning!</h2>
<img src="assets/bc-lego-caveman.jpg" alt="BC Warning" width="70%" data-credit="https://www.flickr.com/photos/pasukaru76/5964727769" />
<p>New reserved class names</p>
<p>Global exception handler</p>
</section>
<section>
<h3>PHP 5</h3>
<pre class="font--24"><code class="php" data-trim>
set_exception_handler(function(\Exception $e) {
// ...
});
</code></pre>
<div class="fragment">
<h3>PHP 7</h3>
<pre class="font--24"><code class="php" data-trim>
set_exception_handler(function(\Throwable $e) {
// ...
});
</code></pre>
</div>
<div class="fragment">
<h3>PHP 5 & 7</h3>
<pre class="font--24"><code class="php" data-trim>
set_exception_handler(function($e) {
// ...
});
</code></pre>
</div>
</section>
</section>
<section>
<!-- Expectations -->
<section>
<h1>#8</h1>
</section>