/
object-type.php
76 lines (54 loc) · 1.1 KB
/
object-type.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
<?php declare(strict_types=1);
dl('redis.so');
/**
* @author hollodotme
*/
class Child
{
private $status = 'I am hungry!';
public function eat() : Child
{
echo "K: Mhh nom nom nom\n";
$this->status = 'I am full up.';
return $this;
}
public function getStatus() : string
{
return $this->status;
}
}
class Baby
{
private $status = 'I am hungry!';
public function eat() : string
{
echo "B: Mhh nom nom nom\n";
return 'I want m`00´re...';
}
public function getStatus() : string
{
return $this->status;
}
}
class GrandMother
{
public function howAboutFood( object $child ) : void
{
echo "G: What about food?\nB: {$child->getStatus()}\n";
}
public function feed( object $child ) : object
{
echo "G: I've got some cookies for you!\n";
return $child->eat();
}
}
$kiddo = new Child();
$granny = new GrandMother();
$granny->howAboutFood( $kiddo );
$fatKiddo = $granny->feed( $kiddo );
$granny->howAboutFood( $fatKiddo );
$baby = new Baby();
$granny = new GrandMother();
$granny->howAboutFood( $baby );
$fatBaby = $granny->feed( $baby );
$granny->howAboutFood( $fatBaby );