· 8 years ago · Jul 20, 2018, 09:34 AM
1<?php
2
3
4class People{ //blueprint, patern, object factory
5
6 protected $skils;
7
8 // it's belongs only to the class
9 static $workingHours = 8;
10
11
12 function __construct($skils) {
13 $this->skils = $skils;
14 //same as top
15 // $this->setSkils($skils);
16
17 // how to work with static variables
18 self::$workingHours --;
19
20 }
21
22 protected function setSkils($value)
23 {
24 $this->skils = $value;
25 }
26 /*
27 * accesors can be: public, private, static, protected
28 */
29
30 //accesors and parameters
31 public $fname;
32 public $age;
33 private $email = "@gmail.com";
34
35 protected $sex = "male";
36
37 // getter
38 function getSex() {
39 return $this->sex;
40 }
41 // setter
42 function setSex($sex) {
43 $this->sex = $sex;
44 }
45
46 function toString() // by default methods == functions are public
47 {
48 return "My info | Name: " . $this->fname . " Age: " . $this->age . "<br>";
49 }
50
51 // reference method
52 function moreInfo()
53 {
54 return "<strong> Hey ... " . $this->toString() . "</strong>";
55 }
56
57 function returnSex() {
58 return "Sex: " . $this->sex . "<br>";
59 }
60
61 // globa getter variable
62 function getProperty($name)
63 {
64 return $this->$name;
65 }
66
67 // global setter variable
68 function setProperty($name, $value)
69 {
70 $this->$name = $value;
71 }
72
73 // magic build in methods
74 function __set($name, $value) {
75 return "The property of: " . $name . "can not be set with value: ". $value;
76 }
77
78 // magic build in methods
79 function __get($name) {
80 return "The propert can not be found with name: " . $name;
81 }
82
83 protected function ddv()
84 {
85 return 0.18;
86 }
87
88 // only working with static properties, static methods or protected
89 static function incrementWorkingHours($value) {
90 $local_var = ((self::$workingHours+= $value)/100) * self::ddv();
91
92 return $local_var;
93 }
94
95
96 static function salaryPerHour($value) {
97 return self::incrementWorkingHours($value) * 2;
98 }
99
100 function __call($method, $args) {
101 echo "<br>The method: " .$method . " does not exist.";
102 foreach ($args as $value)
103 {
104 echo "<br>" . $value . " does not exist";
105 }
106 }
107
108 function __toString() {
109 return "<br>" . $this->fname .", " . $this->age . ", " . $this->sex . "," . $this->skils;
110 }
111
112 function __destruct() {
113 //echo "<br>Destructor is here";
114 }
115}
116
117// final set not inheritable class, you can put it final methods
118//so that methods can't be callable in this class from the parent class
119final class IT_engineer extends People {
120
121 function __construct($skils) {
122 $this->fname = "Mitko";
123 parent::__construct($skils); //you overwrite the __constract function
124 }
125
126 //overwrite method
127 function returnSex() {
128 //calling the parent method
129 //parent::returnSex();
130
131 return "Fe/male sex:" . $this->sex . "<br> PARENT:" . parent::returnSex();
132 }
133
134}
135
136abstract class Viacle{
137
138 protected $cost;
139 protected $weight;
140
141 const REGISTRATION = "MKD";
142
143 function __construct($c,$w) {
144 $this->cost = $c;
145 $this->weight = $w;
146 }
147
148 abstract function toString();
149}
150
151// interfecaes have only constants
152interface carInfo
153{
154 const NUMBER = 2;
155
156 function height($numPeople);
157
158
159}
160
161trait CarWindows {
162
163 protected $typeOfGlass;
164
165 function costTypeOfGlass($value) {
166 $this->typeOfGlass = $value;
167 }
168}
169
170
171class Car extends Viacle implements carInfo {
172
173 //to add trait to class
174 use CarWindows;
175
176 protected $color;
177
178
179
180 function __construct($color, $cost, $weight) {
181 $this->color = $color;
182 parent::__construct($cost, $weight);
183 }
184
185 public function toString()
186 {
187 echo "<br> CAR INFO: <br> COLOR: " . $this->color . ", COST: " . $this->cost . ", WEIGHT: " . $this->weight;
188 }
189 public function height($numPeople)
190 {
191
192 return 2*$numPeople;
193 }
194
195
196}
197// instances
198
199$p = new People("PHP");
200
201$p->fname = "Andrej";
202$p->age = 23;
203
204echo $p->toString();
205
206echo $p->moreInfo();
207
208echo $p->returnSex();
209
210$p->setSex("female");
211echo $p->getSex();
212
213echo "<br>" . $p->getProperty("fname1");
214echo "<br>" . $p->getProperty("age");
215
216$p->setProperty("fname", "Petkana");
217echo "<br>" . $p->toString();
218
219// new instance decrement the static property
220$p1 = new People("Python");
221
222echo $p1->getProperty("skils");
223
224//unset($p1);
225
226echo "WORKING HOURS: " . People::$workingHours;
227
228echo "<br> WORKING HOURS: ". People::incrementWorkingHours(3);
229
230echo "<br> Incrised Salary per Workers: " . People::salaryPerHour(1) . "$";
231
232// for example does not exist.
233echo $p->aaa('aaa',123,'ggg');
234
235echo $p;
236
237echo "<br> INSTANCEOF: " . $p instanceof People;
238
239
240// predifined functions
241
242//var_dump(get_declared_classes());
243//var_dump(class_exists("People"));
244//var_dump(property_exists($p, "fname"));
245//var_dump(get_class_methods($p));
246
247//Inheritance
248$e = new IT_engineer("C++");
249//echo "THE NAME IS: " . $e->getProperty("email");
250//echo $e::$workingHours;
251echo "<br>" . $e->returnSex();
252
253echo $e->getProperty('skils');
254
255$c = new Car("red",200,6000);
256
257$c->toString();
258
259echo "REGISTRATION" . Viacle::REGISTRATION;