forked from Viper-7/Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixin_example.php
105 lines (82 loc) · 2.54 KB
/
mixin_example.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
<?php
/**
* Example code for http://home.viper-7.com/trac/browser/PHP/mixin.php
*
* @Author Dale Horton
* @Date 2009-12-20
*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <viper7@viper-7.com> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Dale Horton
* ----------------------------------------------------------------------------
**/
include('mixin.php');
class Lifeform implements Mixable
{
public $birth_date;
public $living = TRUE;
public function __construct()
{
$this->birth_date = time();
}
public function kill()
{
$this->living = FALSE;
}
}
class Animal implements Mixable
{
public $gender;
public function __construct($gender)
{
$this->gender = $gender;
}
public function walk($legs)
{
echo "I'm walking on $legs legs<br/>";
}
}
class Dog implements Mixable
{
public $breed;
public $legs = 4;
public function bark()
{
echo 'Woof!<br/>';
}
public function walk()
{
// Look for a walk() method in this object's parent classes
// (in this case, it would be Animal's walk() method), call it,
// and pass it the number of legs for this dog
$mixin = Mixin::getMixin($this);
$mixin->parentCall($this, 'walk', $this->legs);
}
}
// Create our mixed class
class My_Dog extends Mixin {}
// Instantiate it
$my_dog = new My_Dog();
// Instantiate a new Lifeform object and mix it into the $my_dog instance
$my_dog->inherit('Lifeform');
// Instantiate a new Animal object passing the gender to the constructor and mix it into the $my_dog instance
$my_dog->inherit('Animal', 'Male');
// Instantiate a new Animal object and mix it into the $my_dog instance, overriding Animal's walk() method with a more specific version
$my_dog->inherit('Dog');
// Output the birth date (from the Lifeform class)
echo 'Birth Date: ' . date('r', $my_dog->birth_date) . '<br/>';
// Output the gender (from the Animal class)
echo 'Gender: ' . $my_dog->gender . '<br/>';
// Call the bark() method (from the Dog class)
$my_dog->bark();
// Call the walk() method (from the Dog class)
$my_dog->walk();
// Output the current status (from the Lifeform class)
echo 'Status: ' . ($my_dog->living?'Alive':'Dead') . '<br/>';
// Update the current status (from the Lifeform class)
echo 'Calling kill()<br/>';
$my_dog->kill();
// Output the current status (from the Lifeform class)
echo 'Status: ' . ($my_dog->living?'Alive':'Dead') . '<br/>';