-
Notifications
You must be signed in to change notification settings - Fork 1
/
oop.php
708 lines (541 loc) · 14.3 KB
/
oop.php
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
<?php
// in this code i will recape the OOP wiht all cases and consepts .
//Hi this is ali ali.
#what is the class with simple explanation.
//the class simply is a container (blueprint) that define new type of varibales into programming language.
//the class is the main idea of the OOP which is the next step of programming after the procedure programming
//OOP is to define code one time and reuse it.DRY(don't repeat yourself)
//the object is the instance of class
//inside the class we define two main things 1-attributes(properites),2-functions which present actions(methods).
//all this info above is just to clarify simply what the class and object.
//fist task define class and create object from this class.
#define class Person
class Person
{
//define Attributes
public $id;
public $name;
public $gender;
public $age;
//define the methods
public function say_hello($name)
{
echo "hi this is " . $name . "\n";
}
public function talk_about_me($name, $id, $age)
{
echo "hi this is " . $name . "and this is my id " . $id . "and this is my age " . $age . "\n";
}
}
#define object and set a value for propertry and call function
// $person1 =new Person();
// $person1->name="ali";
// echo $person1->name;
// echo "\n";
// $person1->say_hello($person1->name);
#TODO
//this
class Car
{
public $type;
public $model;
public $direction;
public function move_forward($direction)
{
$this->direction = $direction;
}
}
// $car1=new Car();
// $car1->type="bmw";
// $car1->move_forward("forward");
// echo "the diretion of the ".$car1->type." is ".$car1->direction;
#TODO
//constructor
class Flower
{
public $name;
public $price;
//define constructor wihtout parameters
public function __construct()
{
$this->name = "rose";
$this->price = 15;
}
}
// $flower1=new Flower ();
// echo "the flower1 is ".$flower1->name." and it's price is ".$flower1->price;
//define constructor with argument
class Plan
{
public $type;
public $speed;
public $high;
public function __construct($type, $speed)
{
$this->type = $type;
$this->speed = $speed;
}
}
// $plan1=new Plan("emarites_19",200000);
// echo "the speed of the ".$plan1->type." plan is ".$plan1->speed;
#TODO
//set,get normal methods
class Cat
{
public $name;
public $age;
public $sound;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
public function make_meow($name, $sound)
{
echo "i am " . $name . " i am a cat " . "and this is my sound " . $sound;
}
public function set_name($name)
{
$this->name = $name;
}
public function set_sound($sound)
{
$this->sound = $sound;
}
public function get_name()
{
return $this->name;
}
public function get_sound()
{
return $this->sound;
}
public function get_age()
{
return $this->age;
}
}
// $cat1=new Cat("meky",3);
// $cat1->set_sound("MEOOOO");
// $cat1->make_meow($cat1->name,$cat1->sound);
// echo " and this is my age ".$cat1->age." months .";
#TODO
//destructor
// the constructor in useless until now ... in my openion
class Fly
{
public $time;
public $speed;
public function __construct($time, $speed)
{
$this->time = $time;
$this->speed = $speed;
}
public function __destruct()
{
echo "this the fly to germeny and its time is " . $this->time . " and its speed is " . $this->speed;
}
}
// $germeny_flight=new Fly("12:00",2000);
// the construct when i click run and the program exits.
#TODO
//inheritance
// class Fruit{
// public $name;
// public $price;
// public function __construct($name,$price){
// $this->name=$name;
// $this->price=$price;
// }
// public function buy_fruit($name){
// echo "this ".$name." is a good fruit"." it's price is ".$this->price;
// }
// }
// class Apple extends Fruit{
// }
// $apple1=new Apple("apple",1);
// $apple1->buy_fruit($apple1->name);
#TODO
//overriding which is a form of polymorphsim
class Fruit
{
public $name;
public $price;
public $colour;
public function __construct($name, $price)
{
$this->name = $name;
$this->price = $price;
}
public function buy_fruit($name)
{
echo "this " . $name . " is a good fruit" . " it's price is " . $this->price;
}
}
class Apple extends Fruit
{
//the normal function should be compitable with the orginal function in th father class
public function buy_fruit($name)
{
echo "this is an " . $name . " it's price is " . $this->price;
}
//while the constructor could be different in it's signiture
public function __construct($name, $price, $colour)
{
$this->name = $name;
$this->price = $price;
$this->colour = $colour;
}
}
// $apple1=new Apple("apple",3,"green");
// $apple1->buy_fruit($apple1->name);
// echo " and my colour is ".$apple1->colour;
#TODO
//self
#the self keyword is used to refer to the current class.
#It's often used in static contexts where $this (which refers to the current object instance) is not available.
#self is primarily used to access static properties, static methods, and constants of a class.
#TODO
//constant
// class HowAreU{
// public $name;
// public $good;
// const welcom_message="هايز حبايب " ;
// const byby_message="بيسسس";
// }
// accses the constant from outside class
// echo HowAreU::welcom_message;
// now access the constant inside the class
class HowAreU
{
public $name;
public $good;
const welcom_message = "هايز حبايب "; // recomend to be upper case. all of it
const byby_message = "بيسسس";
public function __construct($name, $good)
{
$this->name = $name;
$this->good = $good;
echo self::welcom_message;
}
public function say_bye($name)
{
echo self::byby_message;
}
}
// $ali=new HowAreU("ALi",True);
// echo "\n";
// $ali->say_bye($ali->name);
#TODO
//final
// The final keyword can be used to prevent class inheritance or to prevent method overriding.
// final class Dog{
// public $name;
// public $price;
// public $sound;
// public function __construct($name){
// $this->name=$name;
// }
// }
// // will give error.
// class Boby extends Dog {
// }
// class Butterfly{
// public $name;
// public $colour;
// final function __construct($name,$colour)
// {
// $this->name=$name;
// $this->colour=$colour;
// }
// }
// class Smallbutterfly extends Butterfly{
// // the message error is Cannot override final method Butterfly::__construct()
// public function __construct()
// {
// }
// public function fly(){
// echo "hi this is ".$this->name;
// }
// }
// $butterfly1=new Smallbutterfly("soso","white");
// $butterfly1->fly();
#TODO
//access modifiers.
// 1-Public: Public properties or methods can be accessed from anywhere -
// outside the class, within the class itself, and from child classes.
// 2-Protected: protectected properties can be accessed inside the class itself and and from child of ths class
// but we cann't reach it from outside the class .
//3-private :we can reach it from the class it self just .
#TODO
//SETTER,GETTER magic methods i don't think this is OOP :)
/*1. Purpose of __get() and __set()
The “__get()” and “__set()” methods are primarily used to control access to object properties.
When an attempt is made to “access a property“ that is not accessible
or does not exist, __get() is triggered.
Similarly, when an attempt is made to “assign a value“ to an inaccessible or
nonexistent property, __set() is invoked.*/
// implement __set and __get
// first the set method
class ship
{
public $space;
private $colour = "blue";
public function __construct($space)
{
$this->space = $space;
}
// after this function the class or even the current object will not add to the new property into it's properties.
public function __set($property_name, $property_value)
{
if (!property_exists(__CLASS__, $property_name)) {
echo "this property " . $property_name . " is not exsit u are trying to assign value for non exist property" . " and its value now is " . $property_value;
} else {
$this->colour = $property_value;
echo "you are tring to reach inaccecable property but you can change it's value :)\n ";
echo "the new value of the colour which is private porperty is " . $this->colour;
}
}
}
// $ship1=new ship(200);
// $ship1->colour="green"; #assign value for private and break the rule using the magic __SET cooooollll
//the get method
class Tree
{
public $age;
protected $type = "apple-tree";
public function __construct($age)
{
$this->age = $age;
}
public function __get($property_name)
{
if (!property_exists(__CLASS__, $property_name)) {
echo "this property is not exist";
} else
echo $this->{$property_name}; //new syntax here {$....}
}
}
// $tree1=new Tree(2);
// $tree1->type;
#TODO
//static methods
class Bmw
{
public $model;
public $speed;
public function __construct($model)
{
$this->model = $model;
}
// the main idea for use static to create another construcotr. is we can do more operation before intialize another constructor.
public static function create_another_constructor_break_rules($model)
{ // the case that it's kind of weakness the number of argument coudn't change.
// we can define condiction befor intialization and don't change the main constructor.
if ($model == "mercedes")
return new self($model);
elseif ($model == "toyta")
return new self($model);
/* another operatiion we can apply .
public static function withDifferentOperators($value, $anotherValue) {
$instance = new self($value);
$instance->anotherProperty = $value + $anotherValue;
return $instance;
}
*/
}
}
// $bmw1=new Bmw("bmw");
// $bmw2=Bmw::create_another_constructor_break_rules("mercedes");
// echo "the model is ".$bmw2->model;
#TODO
//static properties
#case1 the normal case which is the reach the static property form the outside the class
class Sproperty
{
public static $name = "aliali";
public static $weight = 79;
}
// echo Sproperty::$name;
// echo "\n";
// ectho Sproperty::$weight;
#case2: use the static property inside the class using the self keyword.
class Stapro
{
public static $property1 = "sea";
public static $property2 = "sky";
public $property3;
public function __construct($p)
{
Stapro::$property1 = $p;
}
}
// $test_pro=new Stapro("ocean");
// echo "".Stapro::$property1."\n";
#case 3 extend form the class and use static property by using the parent keywords.
class A
{
public static $pi = 3.14;
public function __construct($pi)
{
A::$pi = $pi;
}
}
class B extends A
{
function change_pi($pi)
{
parent::$pi = $pi;
}
}
// $b=new B(3.15);
// echo "the pi value from A class befor echnaging it's value is ".A::$pi;
// echo "\n";
// $b->change_pi(3.27);
// echo "the pi va;lue is form B class ".B::$pi;
// echo "\n";
// echo "the pi value from A class is ".A::$pi;
// echo "\n";
#TODO
//anonyomous class
// which will we use it for mermory and for temporary classes .
//simply we use the same way when we deal with the anonymous class .
//instead of using the user defined classes we directly use this
$obj = new class()
{
//property
public $name = "aliRali";
public function hello_ali()
{
echo "hello " . $this->name;
}
};
// $obj->hello_ali();
//types of polymorphism
#TODO
//interface with some encapsolation
interface Payment
{
public function payment_process();
}
class Visa implements Payment
{
public function loginfirst()
{
echo "when us want to use the visa u should to login first";
}
public function paynow(): string
{
return "this is visa";
}
public function payment_process()
{
$this->loginfirst();
$payment_method = $this->paynow();
echo $payment_method;
}
}
class Cash implements Payment
{
public function paynow(): string
{
return "this is cash";
}
public function payment_process()
{
$payment_method = $this->paynow();
echo $payment_method;
}
}
class Paypal implements Payment
{
public function paynow(): string
{
return "this is paypal";
}
public function payment_process()
{
$payment_method = $this->paynow();
echo $payment_method;
}
}
class BuyProduct
{
public function make_payment(Payment $payment)
{
$payment->payment_process();
}
}
// $cash=new Cash();
//new commit
// $buyproduct=new BuyProduct();
// $buyproduct->make_payment($cash);
#TODO
//abstract
abstract class Shape
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
public function setname($name)
{
$this->name = $name;
}
abstract public function sayname();
}
class Rectangle extends Shape
{
public function sayname()
{
echo "this shape is " . $this->name;
}
}
class Circle extends Shape
{
public function sayname()
{
echo "this shape is " . $this->name;
}
}
// $circle =new Circle("CIRCLE");
// $rectangle=new Rectangle("RECTANLGE");
// $circle->sayname();
// echo "\n";
// $rectangle->sayname();
#TODO
//traits
/*
PHP only supports single inheritance: a child class can inherit only from one single parent.
So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem
*/
trait Screen
{
public $type;
public $price;
public function __construct($type, $price)
{
$this->type = $type;
$this->price = $price;
}
public function knowtype(): string
{
return $this->type;
}
}
class MobScreen
{
use Screen;
}
class LabScreen
{
use Screen;
}
// $mobile = new MobScreen("mobile", 20000);
// $labtop = new LabScreen("labtop", 4000);
// $mobile_type = $mobile->knowtype();
// echo $mobile_type;
// echo "\n";
// $labtop_type = $labtop->knowtype();
// echo $labtop_type;