-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrait.php
More file actions
55 lines (35 loc) · 1016 Bytes
/
trait.php
File metadata and controls
55 lines (35 loc) · 1016 Bytes
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
<?php
interface Person {
public function greet();
public function eat($food);
}
interface Person2 {
public function eat2($food);
}
trait EatingTrait {
public function eat($food){
$this->putInMouth($food);
}
public function eat2($food){
$this->putInMouth($food);
}
private function putInMouth($food){
echo 'Eat '.$food.'<br/>';
}
}
// We can use Trait if we don't want to provide body for the interface methods in current class, instead we can use the trait and provide body for the methods of interface methods.
class NicePerson implements Person, Person2 {
use EatingTrait;
public function greet(){
echo 'Good day, good sir!';
}
}
$person = new NicePerson;
$person->eat('Egg');
$person->eat('Chicken');
// class MeanPerson implements Person{
// use EatingTrait;
// public function greet(){
// echo 'Your mother was a hamster!';
// }
// }