forked from dbierer/php-class-notes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphp-cert-apr-2023.md.orig
1080 lines (990 loc) · 24.5 KB
/
php-cert-apr-2023.md.orig
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
# PHP Certification -- Apr 2023
<<<<<<< HEAD
## TODO
* Documentation reference on "caller information"
## Homework
=======
Last: http://localhost:8884/#/9/21
## Q & A
* Good discussion of `ArrayObject` using `STD_PROP_LIST` or `ARRAY_AS_PROPS`:
* See: https://stackoverflow.com/questions/14610307/spl-arrayobject-arrayobjectstd-prop-list
## Homework
For Fri 14 Apr 2023
* Quiz questions for Topic #8 (Databases)
* Quiz questions for Topic #9 (Security)
* Quiz questions for Topic #10 (Web)
* Quiz questions for Topic #11 (Errors)
* Final Mock Exam (self study)
For Wed 12 Apr 2023
* Quiz questions for Topic #7 (OOP)
Mock Exam #2
For Tue 11 Apr 2023
* Quiz questions for Topic #4 (Arrays)
* Quiz questions for Topic #5 (I/O)
* Quiz questions for Topic #6 (Functions)
Mock Exam #1
For Thu 6 Apr 2023
* Quiz questions for Topic #2 (Data Formats and Types)
* Quiz questions for Topic #3 (Strings and Patterns)
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
For Wed 5 Apr 2023
* Quiz questions for Topic #1 (Basics)
## Class Notes
* Topic Areas: https://www.zend.com/training/php-certification-exam
* PHP Source Code: https://github.com/php/php-src
* Magic constants: https://www.php.net/manual/en/language.constants.magic.php
* When would you use an alias along with `use`
```
<?php
namespace X {
class Test {}
}
namespace Y {
class Test {}
}
namespace Z {
// must use alias, otherwise PHP doesn't know which "Test" you're referring to
use X\Test as XT;
use Y\Test as YT;
$test = new XT();
}
```
* Type coercion / type juggling
* Study this example:
```
<?php
$a = 123;
$b = 456;
$c = '789';
// in the case combined operations, the last one wins
$e = $a + $b . $c;
var_dump($e); // string(579789)
$e = $a . $b + $c;
var_dump($e); // int(124245)
// the data type of $b is changed because of the string operators "."
$b = $a . '+' . $b;
var_dump($b);
// the data type of $c is "juggled" temporarily to int to satisfy the operation
$d = $a + $c;
var_dump($c);
```
* Another example:
```
<?php
$a = 11;
$b = 22;
$c = 'The sum is: ' . $a + $b;
var_dump($c);
// In PHP 7.1:
/*
PHP Warning: A non-numeric value encountered in /srv/code/test.php on line 4
int(22)
*/
// IMPORTANT: the result in PHP 8 is different!
// string(14) "The sum is: 33"
```
* Assignment Operator
* IMPORTANT: all object assignments are by reference (unless you use keyword `clone`)
```
<?php
class Test
{
public $value = 1;
}
$a = new Test();
$b = $a; // all object assignments are by reference!
$b->value = 2;
var_dump($a, $b);
// output:
/*
/srv/code/test.php:9:
class Test#1 (1) {
public $value =>
int(2)
}
/srv/code/test.php:9:
class Test#1 (1) {
public $value =>
int(2)
}
*/
```
* Example of left/right shift
```
<?php
$three = 0b00000011; // 3
// 3 << 5
$final = 0b01100000; // 96
// 96 >> 5
$final = 0b00000011; // 4
// same thing but using 7
$three = 0b00000111; // 7
// 3 << 5
$final = 0b11100000; // 224
// 224 >> 6
$final = 0b00000011; // 3
```
## Docker Container Setup
* Download the ZIP file via Zoom
* Unzip into a new folder `/path/to/zip`
* Follow the setup instructions in `/path/to/zip/README.md`
## Q & A
* Q: How do I know if an extension is part of the "core"
* A: See: https://github.com/php/php-src/tree/php-7.1.33/ext
* These are all core
* Not all are enabled by default
* Only the ones enabled will be on the test
What's the difference between `define()` and `const` for constants:
```
<?php
namespace x {
define('TEST1', 'xyz');
const TEST2 = 'abc';
}
namespace y {
function test()
{
return TEST1 . TEST2;
}
echo test();
// output: xyzTEST2
}
```
Namespaces:
* Cannot have keywords in the namespace in PHP 7.1
```
namespace Test\List\Whatever;
use ArrayObject;
$obj = new ArrayObject([1,2,3,4,5]);
var_dump($obj);
// PHP Parse error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING) in /srv/code/test.php on line 2
```
IMPORTANT: when assigning objects, it's automatically by reference (even without the `&`)
```
<?php
$obj = new stdClass();
$obj->name = 'TEST';
$abc = $obj;
$abc->name = 'Whatever';
echo $obj->name;
echo PHP_EOL;
// Output: "Whatever"
```
What is considered "empty"?
* https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
* Overview of topics: https://www.zend.com/training/php-certification-exam
* Good overview of typical PHP program operation:
* https://www.zend.com/blog/exploring-new-php-jit-compiler
Constants
```
<?php
namespace abc {
define('WHATEVER', 'Whatever', TRUE);
const ANYTHING = 'Anything';
}
namespace xyz {
echo WHATEVER;
echo ANYTHING;
}
```
### Bitwise Operators
Tutorial oriented towards the exam:
* https://www.w3resource.com/php/operators/bitwise-operators.php
* Truth table:
```
<?php
echo "Logical AND\n";
printf("%04b\n", 0b00 & 0b00); // 0
printf("%04b\n", 0b00 & 0b01); // 0
printf("%04b\n", 0b01 & 0b00); // 0
printf("%04b\n", 0b01 & 0b01); // 1
echo "Logical OR\n";
printf("%04b\n", 0b00 | 0b00); // 0
printf("%04b\n", 0b00 | 0b01); // 1
printf("%04b\n", 0b01 | 0b00); // 1
printf("%04b\n", 0b01 | 0b01); // 1
echo "Logical XOR\n";
printf("%04b\n", 0b00 ^ 0b00); // 0
printf("%04b\n", 0b00 ^ 0b01); // 1
printf("%04b\n", 0b01 ^ 0b00); // 1
printf("%04b\n", 0b01 ^ 0b01); // 0
```
Examples of the three ops:
```
<?php
$a = 0b11111111;
$b = 0b11011101;
printf("%08b", $a & $b); // 1101 1101
printf("%08b", $a | $b); // 1111 1111
printf("%08b", $a ^ $b); // 0010 0010
```
Left/right shift illustration:
```
<?php
echo 16 << 3;
echo "\n";
echo 0b10000000;
echo "\n";
echo 16 >> 3;
echo "\n";
echo 0b00000010;
echo "\n";
echo 15 >> 3;
echo "\n";
echo 0b00000001;
echo "\n";
```
Nested Ternary Construct
```
$a = 30;
$b = 20;
echo ($a < $b) ? 'Less' : (($a == $b) ? 'Equal' : 'Greater');
// output: "Greater"
```
Null coalesce operator example
```
$token = $_GET['token'] ?? $_POST['token'] ?? $_COOKIE['token'] ?? 'DEFAULT';
```
Yet another example
```
<?php
// example of null coalesce operator
// first expression is the 1st CLI arg
// if that's not present, looks to the URL or post
$action = $argv[1] ?? $_GET['action'] ?? $_POST['action'] ?? 'nothing';
```
<<<<<<< HEAD
=======
Switch statement example
```
<?php
$a = '1';
switch ($a) {
case 1 :
echo 'A';
break;
case '2' :
echo 'B';
break;
default :
echo 'C';
}
// output: "A" because switch does a non-strict comparison
// NOTE: if the "break;" is missing, it would keep running code
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
`php.ini` file settings:
* https://www.php.net/manual/en/ini.list.php
Extensions
* These are in the core:
* https://github.com/php/php-src/tree/PHP-7.1.30/ext
* *but* not all are enabled by default
* You're only tested on the extensions enabled by default
## Garbage Collection
* Study up on `gc_collect_cycles()`
Have a look at this article:
https://www.php.net/manual/en/features.gc.performance-considerations.php
## Data Formats
Read up on `SimpleXMLElement`
* https://www.php.net/manual/en/simplexml.examples-basic.php
* Simple example
```
<?php
$xml = <<<EOT
<topics>
<topic id="1">XML</topic>
<topic id="2">Web Services</topic>
<topic id="3">Whatever</topic>
<info>
<name>Doug</name>
<name>Hudo</name>
</info>
</topics>
EOT;
$simple = new SimpleXMLElement($xml);
echo $simple->info->name; // Doug
echo $simple->info->name[1]; // Hudo
echo $simple->topic[2]; // Whatever
echo $simple->topic[2]['id']; // 3
```
* XPath Tutorial: https://www.w3schools.com/xml/xpath_intro.asp
* `DateTime` examples
```
<?php
// for relative formats see:
// https://www.php.net/manual/en/datetime.formats.relative.php
$date[] = new DateTime('2023-03-16');
$date[] = new DateTime('third thursday of next month');
$date[] = new DateTime('now', new DateTimeZone('CET'));
$date[] = new DateTime('@' . time());
$date[] = (new DateTime())->add(new DateInterval('P3D'));
var_dump($date);
```
<<<<<<< HEAD
=======
* `DateInterval` format codes
* See: https://www.php.net/manual/en/dateinterval.construct.php
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
* Don't forget that to run a SOAP request, you can also use:
* `SoapClient::__soapCall()`
* `SoapClient::__doRequest()`
* Example of a soap client:
* https://github.com/dbierer/classic_php_examples/blob/master/web/soap_client.php
* Study on `DateTimeInterval` and `DateTimeZone` and also "relative" time formats
* In addition, be aware of the basic time format codes
* https://www.php.net/manual/en/datetime.format.php
* Pay close attention to `strftime()`
* https://www.php.net/manual/en/function.strftime.php
* PayPal has a SOAP API that is publically accessible
* REST vs. SOAP:
* See: https://www.ateam-oracle.com/post/performance-study-rest-vs-soap-for-mobile-applications
## Strings
* Be very careful with `strpos()` and `stripos()`
```
<?php
$str = 'The quick brown fox jumped over the fence';
echo '"The" was ';
echo (stripos($str, 'The')) ? 'found' : 'not found';
echo ' in the string ' . $str;
echo PHP_EOL;
// actual output:
// "The" was not found in the string The quick brown fox jumped over the fence
```
* Study `substr()` with negative args
```
<?php
$a = 'test.php';
// test. php
$b = substr($a, 0, -3) . substr($a, -3);
echo ($a === $b) ? 'T' : 'F';
// ouput: "T"
```
* Study the docs on `sprintf()` to get format codes for that family of functions
* Example using negative offsets:
```
<?php
$dir = '/home/doug/some/directory/';
if (substr($dir, 0, 1) === '/') echo 'Leading slash' . PHP_EOL;
if (substr($dir, -1) === '/') echo 'Trailing slash' . PHP_EOL;
if ($dir[-1] === '/') echo 'Trailing slash' . PHP_EOL;
```
<<<<<<< HEAD
=======
* Modifying the values using "de-referencing"
```
<?php
$str = 'abcdef';
echo $str . "\n"; // abcdef
$str[0] ='z';
echo $str . "\n"; // zbcdef
unset($str[2]);
echo $str . "\n"; // Fatal Error
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
* Tutorial on PHP regex: https://www.w3schools.com/php/php_regex.asp
* Using regex to find distinct words (using `\b`)
```
<?php
$str[] = 'This is an example of error_reporting'; // NO MATCH
$str[] = 'ERROR: this is problem'; // MATCH
$patt = '/\bERROR\b/i';
$srch = 'ERROR';
foreach ($str as $item) {
echo $item . "\n";
echo (preg_match($patt, $item)) ? 'MATCH' : 'NO MATCH';
echo "\n";
}
```
<<<<<<< HEAD
=======
* What is the output?
```
<?php
echo '1' . (print '2') + 3;
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
* Using regex to swap sub-patterns
```
<?php
$text = 'Doug Bierer';
$patt = '/(.*)\s(.*)/';
echo preg_replace($patt, '$2, $1', $text);
```
* `preg_replace()` and `preg_match()` example using sub-patterns:
```
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '$2 $1 $3';
echo preg_replace($pattern, $replacement, $string);
preg_match($pattern, $string, $matches);
var_dump($matches);
```
* Same thing, but going from European date format to American
```
<?php
$str = date('d M Y');
// subpatt: 1 2 3
$pat = '/^(\d+?) (\w+?) (\d{4})$/';
$rep = '$2 $1, $3';
echo preg_replace($pat, $rep, $str);
echo PHP_EOL;
// example output: "Mar 16, 2023"
```
Greediness Example:
```
<?php
$str = '<p>Para 1</p><p>Para 2</p><p>Para 3</p>';
<<<<<<< HEAD
// $pat = '!<p>.*</p>!'; // returns the entire string
$pat = '!<p>.*?</p>!'; // returns "<p>Para 1</p>"
=======
//$pat = '!<p>.*</p>!'; // returns the entire string
//$pat = '!<p>.*?</p>!'; // returns "<p>Para 1</p>"
$pat = '!<p>.*</p>!U'; // returns "<p>Para 1</p>"
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
preg_match($pat, $str, $matches);
var_dump($matches);
echo PHP_EOL;
```
<<<<<<< HEAD
=======
Example using "word boundary" (\b)
```
<?php
$str = [
'This program has an ERROR in it',
'ERROR: Big Big Problem!',
'This is just ERROR_REPORTING',
];
$patt = '/\bERROR\b/i';
foreach ($str as $line) {
echo $line . ': ';
echo (preg_match($patt, $line)) ? 'MATCH' : 'NO MATCH';
echo PHP_EOL;
}
// actual output:
/*
MATCH
MATCH
NO MATCH
*/
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
General regex coding examples:
* https://github.com/dbierer/classic_php_examples/tree/master/regex
## Arrays
For iterating through an array beginning-to-end don't forget about these functions:
* `array_walk()`
* `array_walk_recursive()`
* `array_map()`
Also: please don't forget the array *navigation* functions:
* `reset()`: sets pointer to top
* `end()` : sets pointer to end
* `prev()` : advances array pointer
* `next()` : un-advances array pointer
* `key()` : returns index value at array pointer
* `current()` : returns value of element at array pointer
<<<<<<< HEAD
=======
Assignment example:
```
<?php
$a = ['A', 'B', 4 => 'C'];
$a[2] = 'E';
$a[] = 'D';
var_dump($a);
/*
array(5) {
[0] =>
string(1) "A"
[1] =>
string(1) "B"
[4] =>
string(1) "C"
[2] =>
string(1) "E"
[5] =>
string(1) "D"
}
*/
```
In PHP 7, if a numeric array has no index > 0, it will always start with 0
```
<?php
$a = [-6 => 'A', -5 => 'B'];
$a[] = 'C';
var_dump($a);
/*
array(3) {
[-6] =>
string(1) "A"
[-5] =>
string(1) "B"
[0] =>
string(1) "C"
}
*/
```
With associative arrays, the keys are retained automatically regardless of the 4 param of `array_slice()`
```
<?php
$a = ['A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5];
$a1 = array_slice($a, 2); // == [3, 4, 5];
$a2 = array_slice($a, 1, 3); // == [2, 3, 4];
$a3 = array_slice($a, 1, 3, true); // == [1 => 2, 2 => 3, 3 => 4]
var_dump($a1, $a2, $a3);
/*
array(3) {
'C' =>
int(3)
'D' =>
int(4)
'E' =>
int(5)
}
/srv/code/test.php:6:
array(3) {
'B' =>
int(2)
'C' =>
int(3)
'D' =>
int(4)
}
/srv/code/test.php:6:
array(3) {
'B' =>
int(2)
'C' =>
int(3)
'D' =>
int(4)
}
*/
```
BE CAREFUL: there is also a function `array_splice()`
* Operates much like `str_replace()`
Example of `array_combine()`
```
<?php
$keys = range('A','F');
$vals = range(1, 6);
$fin = array_combine($keys, $vals);
var_dump($fin);
/*
array(6) {
'A' =>
int(1)
'B' =>
int(2)
'C' =>
int(3)
'D' =>
int(4)
'E' =>
int(5)
'F' =>
int(6)
}
*/
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
## I/O
Streams
* Don't have to study *all* functions, just certain of the more common ones
* https://www.php.net/streams
* `stream_context_create()`
* `stream_wrapper_register()`
* `stream_filter_register()`
* `stream_filter_append()`
* `stream_socket_client()`
In addition to the informational file functions mentioned, you also have:
* `fileatime()`
* `filemtime()`
* `filectime()`
etc.
## Functions
* Read up on `Closure::bindTo()`
* https://www.php.net/manual/en/closure.bindto.php
* Anonymous function example:
```
<?php
$label = 'Result: ';
$add = function ($a, $b) use ($label) {
return $label . ($a + $b);
};
$sub = function ($a, $b) use ($label) {
return $label . ($a - $b);
};
echo $add(6, 3) . PHP_EOL . $sub(6, 3);
// Result: 9
// Result: 3
```
* Alternative example of `bindTo()`
```
<?php
class Airplane {
public $type;
function __construct(string $type) {
$this->type = $type;
}
function getClosure() {
return function() {
return $this->type;
};
}
}
class X {
public $type = 'X';
}
$airplane1 = new Airplane('Airliner');
$closure1 = $airplane1->getClosure();
echo $closure1(). PHP_EOL;
$closure2 = $closure1->bindTo(new X());
echo $closure2();
```
<<<<<<< HEAD
=======
* `bindTo()` doesn't require the same object type
```
<?php
class Airplane {
public $type;
function __construct(string $type) {
$this->type = $type;
}
function getClosure() {
return function() {
return $this->type;
};
}
}
$airplane1 = new Airplane('Airliner');
$airplane2 = new stdClass();
$airplane2->type = 'TEST';
$closure1 = $airplane1->getClosure();
echo $closure1(). PHP_EOL;
$closure2 = $closure1->bindTo($airplane2);
echo $closure2();
// same results as above
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
## OOP
* Read up on magic methods!
* https://www.php.net/manual/en/language.oop5.magic.php
* Examples: https://github.com/dbierer/classic_php_examples/tree/master/oop/*magic*.php
* Don't worry about any methods added after PHP 7.1
* `__destruct()` called when object goes out-of-scope
* End of program
* `unset()`
* Called in a function and function call ends
* Overwritten
* Interfaces can be used as "free agents" to define functionality
* This example ties an interface into an inheritance hierarchy
```
<?php
interface SetGet {
public function set(string $name, callable $service);
public function get(string $name) : callable;
}
abstract class Container implements SetGet {
protected $services = [];
}
class ServiceContainer extends Container {
/*
public function set(string $name, callable $value) {
$this->services[$name] = $value;
}
*/
public function get(string $name) : callable {
return $this->services[$name];
}
}
$service = function () { return (new DateTime('now'))->format('l, d M Y'); };
$container = new ServiceContainer();
$container->set('today', $service);
echo $container->get('today')();
```
* Autoloading Examples:
* https://github.com/dbierer/classic_php_examples/tree/master/oop
* Look for `oop_autoload*.php`
* Callable
* Examples of what is considered "callable"
* https://github.com/dbierer/classic_php_examples/blob/master/oop/callable_examples.php
<<<<<<< HEAD
=======
* `__isset()` rewritten example:
```
<?php
namespace Container;
class ServiceContainer {
protected $path = '/path';
protected $services = [];
public function __get($name) {
return $this->$name ?? false;
}
public function __isset($name) {
return !empty($this->$name);
}
}
$container = new ServiceContainer();
var_dump($container->adapter);
echo isset($container->adapter) ?? false;
echo $container->path . PHP_EOL;
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
* Iteration
* Lookup these interfaces and understand what they do
* `Traversable`
* Both of the interfaces mentioned `extend` this interface
* `Iterator`
* Introduced earlier
* Requires hard-coded iteration methods
* `IteratorAggregate`
* Passes the buck
* Don't have to hard code iteration methods
* Late static binding
* https://www.php.net/manual/en/language.oop5.late-static-bindings.php
* Serialization example:
```
<?php
class Test
{
public $a = 0;
public $b = 0;
public $c = 'Test';
public $d = [];
public $e = '';
public function __construct(int $a, float $b, string $c, array $d)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
$this->d = $d;
$this->e = md5(rand(1111,9999));
}
public function __sleep()
{
return ['a','b','c','d'];
}
public function __wakeup()
{
$this->e = md5(rand(1111,9999));
}
}
$test = new Test(222, 3.456, 'TEST', [1,2,3]);
var_dump($test);
$str = serialize($test);
echo $str . PHP_EOL;
$obj = unserialize($str);
var_dump($obj);
```
* Type hints
* If `declare(strict_types=1)` is not set, the type hint does a "soft" type cast
```
<?php
function test (int $a, int $b)
{
return $a + $b;
}
echo test(2, 2);
echo PHP_EOL;
echo test('2', '2');
echo PHP_EOL;
echo test(2.666, 2.777);
echo PHP_EOL;
echo test('A', 'B');
echo PHP_EOL;
// actual output:
/*
4
4
4
PHP Fatal error: Uncaught TypeError: Argument 1 passed to test() must be of the type integer, string given, called in /srv/code/test.php on line 13 and defined in /srv/code/test.php:2
*/
```
<<<<<<< HEAD
Example of making object callable:
```
<?php
$sum = new class () {
public $num = 0;
public function __invoke($val) {
$this->num += $val;
}
};
$a = [1, 2, 3, 4, 5, 6, 7, 8];
array_walk($a, $sum);
echo 'Sum of Digits: ' . $sum->num;
// output: 36
```
* See: https://github.com/dbierer/classic_php_examples/blob/master/oop/callable_examples.php
SPL
* Make sure you study:
* `*Iterator*` : just know what they are
* `ArrayIterator` and `ArrayObject` make sure you're up to speed on these!
* Just be aware of the "classic" data structure classes
* Generators
* https://www.php.net/manual/en/class.generator.php
=======
* SPL
* Make sure you study:
* `*Iterator*` : just know what they are
* `ArrayIterator` and `ArrayObject` make sure you're up to speed on these!
* Just be aware of the "classic" data structure classes
* Generators
* https://www.php.net/manual/en/class.generator.php
* Example:
```
<?php
$arr = range(1, 1000000);
$div = 17;
function whatDiv(array $arr, int $div)
{
foreach ($arr as $val) {
if ($val % $div === 0) yield $val;
}
}
$res = whatDiv($arr, $div);
// do something
// this would go in a view script
foreach ($res as $item) echo $item . ' ';
echo PHP_EOL;
echo "\nPeak: " . memory_get_peak_usage();
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
* Late Static Binding
* https://www.php.net/manual/en/language.oop5.late-static-bindings.php
* Traits
* https://www.php.net/manual/en/language.oop5.traits.php
## Database Topic
Fetch Modes:
* Focus on array and object fetch modes
* Study the difference between `bindValue()` and `bindParam()`
* See: https://www.php.net/manual/en/pdostatement.bindvalue.php#80285
## Security Topic
Questions are drawn from here:
* https://www.php.net/manual/en/security.php
Read up on the `crypt()` function
* https://www.php.net/manual/en/function.crypt.php
Make sure you read up on `htmlspecialchars()`
* https://www.php.net/htmlspecialchars
<<<<<<< HEAD
=======
```
<?php
$test = "<br>\"double\" & 'single'";
echo htmlspecialchars($test);
echo PHP_EOL;
echo htmlspecialchars($test, ENT_NOQUOTES);
echo PHP_EOL;
echo htmlspecialchars($test, ENT_QUOTES);
// output:
/*
<br>"double" & 'single'
<br>"double" & 'single'
<br>"double" & 'single'
*/
```
>>>>>>> 4755e3ec0f559e0590611a8835b328185c4ba745
Do a quick read on the `crypt()` function
* `password_hash()` leverages this
* Might be on the test
File upload documentation
* https://www.php.net/manual/en/features.file-upload.php
Security related `php.ini` settings
* `open_basedir`
* `doc_root`
* `user_dir`
* `cgi.force_redirect`
Security validation functions
* is_*()
* Checks the actual data type of the variable
* ctype_*()
* This family checks the actual *contents* of the variable
* filter*()
Read up especially on `filter_var()`
* https://www.php.net/filter_var
## Web Features
Make sure you're up on the `php.ini` settings pertaining to web features
URL: https://www.php.net/manual/en/ini.list.php
* `variables_order`
* `request_order`
* `memory_limit`
* `post_max_size`
* `upload_max_filesize`
* `file_uploads`
* `max_file_uploads`
Form postings