-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic OOP
More file actions
712 lines (585 loc) · 11.3 KB
/
Basic OOP
File metadata and controls
712 lines (585 loc) · 11.3 KB
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
<?php
class Fruit {
//Properties
public $name;
public $color;
//Methods
function set_name($name){
$this->name = $name;
}
function set_name(){
return $this->name;
}
function set_color($color){
$this->color = $color;
}
function set_color(){
return $this->color;
}
}
$apple = new Fruit();
$apple->set_name('Apple');
$apple->set_color('Red');
echo "Name: ".$apple->get_name();
echo "<br>";
echo "Color: ".$apple->get_color();
?>
<?php
// inside 0f class
class Fruit{
public $name;
function set_name($name){
$this->name = $name;
}
}
$apple = new Fruit();
$apple->set_name("Apple");
echo $apple->name;
?>
<?php
// outsite of class
class Fruit{
public $name;
}
$apple = new Fruit();
$apple->name = "Apple";
echo $apple->name;
?>
<?php
$apple = new Fruit();
var_dump($aplle instanceof Fruit);
?>
<?php
// __Construct Functions
class Fruit{
public $name;
public $color;
function __construct($name){
$this->name = $name;
}
function get_name(){
return $this->name;
}
}
$apple = new Fruit("Apple");
echo $apple->get_name();
?>
<?php
// __Construct Functions
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function get_name(){
return $this->name;
}
function get_color(){
return $this->color;
}
}
$apple = new Fruit("Apple", "Red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>
<?php
//__destruct Function
class Fruit{
public $name;
public $color;
function __construct($name){
$this->name = $name;
}
function __destruct(){
echo "The fruit is {$this->name}.";
}
}
$aplle = new Fruit("Apple");
?>
<?php
class Fruit{
public $name;
public $color;
function __construct($name, $color){
$this->name = $name;
$this->color = $color;
}
function __destruct(){
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
$apple = new Fruit("Apple", "Red");
?>
<?php
//Properties Modifier Access
class Fruit{
public $name;
protected $color;
private $weight;
}
$mango = new Fruit();
$mango->name = 'Mango'; // OK
$mango->color = 'Green'; // error
$mango->weight = '300' //ERROR
?>
<?php
//Method Modifier Access
class Fruit{
public $name;
public $color;
public $weight;
function set_name($n) { // a public function (default)
$this->name = $n;
}
protected function set_color($n) { //a protected function
$this-color = $n;
}
private function set_weight($n) { // a private function
$this->weight = $n;
}
}
$mango = new Fruit();
$mango->set_name("Mango"); //OK
$mango->set_color("Red"); //error
$mango->set_weight("300"); //ERROR
?>
<?php
//Inheritance useing extends keyword
class Fruit{
public $name;
public $color;
public function __construct($name, $color){
$this->name = $name;
$this->color = $color;
}
public function intro(){
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
// Strawberry is inherited form Fruit
class Strawberry extends Fruit {
public function message(){
echo "Am I a fruit or berry? "
}
}
$strawberry = new Strawberry("Strawberry", "Red");
$strawberry->intro();
$strawberry->message();
?>
<?php
//Inheritance and the protected access modifier
class Fruit{
public $name;
public $color;
public function __construct("$name", "color"){
$this->name = $name;
$this->color = $color;
}
protected function intro(){
echo "This fruit is {$this-name} and the color is {$this-color}.";
}
}
class Strawberry extends Fruit {
public function message(){
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "Red");
$strawberry->message(); //OK message() is public
$strawberry->intro(); //error. intro() is protected
?>
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color){
$this->name = $name;
$this->color = $color;
}
protected function intro(){
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message(){
echo "Am I fruit or a berry?";
// Call protected method from within derived class - OK
$this-> intro();
}
}
$strawberry = new Strawberry("Strawberry", "Red");
$strawberry->message(); //OK. message() is public and it calls intro() (whic is protected) from within the derived class
?>
<?php
//Overridding Inheritance Methodes
class Fruit{
public $name;
public $color;
public function __costruct($name, $color){
$this->name = $name;
$this->color= $color
}
public function intro(){
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public $weight;
public function __construct($name, $color, $weight){
$this->name = $name;
$this->color= $color;
$this_weight= $weight;
}
public function intro(){
echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
}
}
$strawberry = new Strawberry("Strawberry", "REd", 50);
$strawberry->intro();
?>
<?php
//Final keyword
class Fruit{
final public function intro(){
// some code
}
}
class Strawberry extends Fruit{
//will result in error
public function intro(){
//some code
}
}
?>
<?php
// constanct keywork "const" [outside of class]
class Goodby{
const GIVING_MESSAGE = "Thank yu for learn me";
}
echo Goodby::GIVING_MESSAGE;
?>
<?php
// constanct keywork "const" [inside of class by "self"]
class Goodby {
const LIVING_MESSAGE = "Thank you for teach me";
public function byby(){
echo self::LIVING_MESSAGE;
}
}
$goodby = new Goodby();
$goodby->byby();
?>
<?php
//abstract class and method
abstract class ParentClass{
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() :string;
}
?>
<?php
//Abstract class and method
abstract class Car{
public $name;
public function __construct($name){
$this->name = $name;
}
abstract public function intro() : string;
}
// Child class
class Audi extends Car{
public function intro() : string{
return "Choose German quality! I'm an $this->name!";
}
}
class Volvo extends Car{
public function intro() : string {
return "Proud to be swedish! I'm a $this->name! ";
}
}
class Citroen extends Car{
public function intro(){
return "French extravagance! I'm a $this->name!";
}
}
//Create object from the child classes
$audi = new audi("Audi");
echo $audi->intro();
echo "<br>";
$volvo = new volvo("Volvo");
echo $volvo->intro();
echo "<br>";
$citroen = new citroen("Citroen");
echo $citroen->intro();
?>
<?php
// Another example of abstract class
abstract class ParentClass{
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass{
public function prefixName($name){
if ($name == "John Doe") {
$prefix = "Mr.";
}elseif($name == "Jane Doe"){
$prefix = "Mrs.";
}else {
$prefix = "";
}
return "{$prefix} {$name}";
}
}
$class = new ChildClass;
echo $class->prefixName(John Doe);
echo $class->prefixNmae(Jane Doe);
?>
<?php
abstract class ParentClass{
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass{
public function prefixName($name, $separator=".", $greet = "Dear") {
if($name== "John Doe"){
$prefix = "Mr";
}elseif($name == "Jane Doe"){
$prefix = "Mrs";
}else{
$prefix = "";
}
return "{$sreet} {$prefix}{$separator} {$name}";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo $class->prefixName("Jane Doe");
?>
<?php
//Interfacde
interface Animal{
public function makeSound();
}
class Cat implements Animal{
public function makeSound(){
echo "Meow";
}
}
$animal = new Cat();
$animal->makeSound();
?>
<?php
interface Animal{
public function makeSound();
}
class Cat implements Animal{
public function makeSound(){
echo "Meow";
}
}
class Dog implements Animal{
public function makeSound(){
echo "Bark";
}
}
class Mouse implements Animal{
public function makeSound(){
echo "Squeak";
}
}
//create a list of animals
$cat = new Cat();
$dog = new Dog();
$mouse=new Mouse();
$animals = array($cat, $dog, $mouse);
foreach($asnimals as $animal){
$animal->makeSound();
}
?>
<?php
//Traits keyword
trait message1 {
public function msg1(){
echo "OOp is fun!";
}
}
class Welcome{
use message1;
}
$boj = new Welcom();
$boj->msg1();
?>
<?php
trait message1{
public function msg1(){
echo "OOP is fun!";
}
}
trait message2{
public function msg2(){
echo "OOP reduce code duplication!";
}
}
class Welcome{
use message1;
}
class Welcome2 {
use message1, message2;
}
$obj = new Welcome();
$obj->msg1();
echo "<br>";
$obj = new Welcome2();
$obj->msg1();
$obj->msg2();
?>
<?php
//Static Methods
class ClassName{
public static function Welcome(){
echo "WElcome to the channel";
}
}
ClassName::Welcome();
?>
<?php
//Using self keyword
class Greeting{
public static function welcome(){
echo "HI Gusy";
}
public function __construct(){
self::welcome();
}
}
new Greeting();
?>
<?php
class greeting{
public static function welcome(){
echo "Hello World";
}
}
class fromotherClass{
public function msg(){
greeting::welcome();
}
}
?>
<?php
class domain{
public static function getWebSite(){
return "jaak.com";
}
}
class domainchild extends domain{
public $website;
public function __construct(){
$this-website = parent::getWebSite();
}
}
$domainW = new domainchild;
echo $domainW->website;
?>
<?php
//Static Properties
class hi {
public static $value=4.5555;
public function staticvalue(){
return self::value;
}
}
$hi = new hi();
echo $hi->staticvalue();
?>
<?php
//Inside child class call parent keyword
class hi {
public static $value=45.5455;
}
class hello extends hi {
public function xStatic(){
return parent::$value;
}
}
echo x::value;
$x= new x();
echo $x->xStatic();
?>
<?php
//namespace
namespace html;
class Table{
public $title = "";
public $numRows = 0;
public function message(){
echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>";
}
}
$table = new Table();
$table->title = "My table";
$table->numRows = 0;
?>
<!DOCTYPE html>
<html>
<body>
<?php $table->message(); ?>
</body>
</html>
<?php
//Iterable keyword
function printIterable(iterable $myIterable){
foreach($myIterable as $item){
echo $item;
}
}
$arr = ["a", "b", "c"];
printIterable($arr);
?>
<?php
//Return Example
function getIterable():iterable{
return ["a", "b", "c"];
}
$myIterable = getIterable();
foreach($myIterable as $item){
echo $item;
}
?>
<?php
//Creating Iterator - Implement the Iterator interface
class MyIterator implements Iterator{
private $items = [];
pirvate $pointer = 0;
public function __construct($items){
//arrya_value() makes sure that the keys are numbers
$this->items = array_values($itmes);
}
public function current(){
return $this->items[$this->pointer];
}
public function key(){
return $this->pointer;
}
public function next(){
$this->pointer++;
}
public function rewind(){
$this->pointer = 0;
}
public function valid(){
//count() indicates how many items are in the list
return $this->pointer < count($this->items);
}
}
// A function that uses iterables
function printIterable(iterable $myIterable){
foreach($myIterable as $item){
echo $item;
}
}
// Use the iterator as an iterable
$iterator = new MyIterator (["a", "b", "c"]);
printIterable($iterator);
?>