File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed
src/php-70/anynomous-classes Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * PHP 7 Anonymous Claasess
4+ * https://www.tutorialspoint.com/php7/php7_anonymous_classes.htm
5+ */
6+
7+ declare (strict_types = 1 );
8+
9+ interface Logger {
10+ public function log (string $ msg );
11+ }
12+
13+ class Application {
14+ private $ logger ;
15+
16+ public function getLogger (): Logger {
17+ return $ this ->logger ;
18+ }
19+
20+ public function setLogger (Logger $ logger ) {
21+ $ this ->logger = $ logger ;
22+ }
23+ }
24+
25+ // another style to set anynomous class
26+ $ anynomousClass = new class implements Logger {
27+ public function log (string $ msg ){ print ($ msg ); }
28+ };
29+
30+ $ app = new Application ;
31+ $ app ->setLogger ($ anynomousClass );
32+
33+ $ app ->getLogger ()->log ("My first Log Message " );
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * PHP 7 Anonymous Claasess
4+ * https://www.tutorialspoint.com/php7/php7_anonymous_classes.htm
5+ */
6+
7+ declare (strict_types = 1 );
8+
9+ interface Logger {
10+ public function log (string $ msg );
11+ }
12+
13+ class Application {
14+ private $ logger ;
15+
16+ public function getLogger (): Logger {
17+ return $ this ->logger ;
18+ }
19+
20+ public function setLogger (Logger $ logger ) {
21+ $ this ->logger = $ logger ;
22+ }
23+ }
24+
25+ $ app = new Application ;
26+ $ app ->setLogger (new class implements Logger {
27+ public function log (string $ msg ) {
28+ print ($ msg );
29+ }
30+ });
31+
32+ $ app ->getLogger ()->log ("My first Log Message " );
You can’t perform that action at this time.
0 commit comments