-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyEvalutableObject.php
More file actions
134 lines (127 loc) · 3.52 KB
/
LazyEvalutableObject.php
File metadata and controls
134 lines (127 loc) · 3.52 KB
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
<?php
/**
* LazyEvalutableObject
*
* <code>
* class Test extends LazyEvalutableObject {
* public $omase = 'san';
* public $tokyo = 'kyoto';
* public $one_time;
* public function __construct() {
* $this->addLazyEvalutionObserver('omase', array($this, 'omase'));
* $this->addLazyEvalutionObserver('tokyo', array($this, 'tokyo'));
* $this->addLazyEvalutionObserver('one_time', array($this, 'one_time'));
* }
* public function omase($property) {
* echo sprintf("hello! property is %s\n", $property);
* return LazyEvalutableObject::DEFAULT_VALUE;
* }
* public function tokyo($property) {
* echo sprintf("hello! property is %s\n", $property);
* return 'osaka';
* }
* public function one_time($property) {
* echo sprintf("hello! property is %s\n", $property);
* $this->one_time = rand(0, 10);
* return $this->one_time;
* }
* }
*
* $test = new Test();
* echo sprintf("\$test->omase = %s\n", $test->omase);
* // hello! property is omase
* // $test->omase = san
*
* echo sprintf("\$test->tokyo = %s\n", $test->tokyo);
* // hello! property is tokyo
* // $test->tokyo = osaka
*
* echo sprintf("\$test->one_time = %s\n", $test->one_time);
* echo sprintf("\$test->one_time = %s\n", $test->one_time);
* // hello! property is one_time
* // $test->one_time = 1
* // $test->one_time = 1
* </code>
*
* @author hackoh
*
*/
abstract class LazyEvalutableObject {
/**
*
* @var string
*/
const DEFAULT_VALUE = '___LazyEvalutableObject::DEFAULT_VALUE___';
/**
*
* @access private
* @var array
*/
private static $_events = array();
/**
*
* @access private
* @var array
*/
private static $_defaults = array();
/**
* unique hash (for >PHP5.2)
*
* @var string
*/
private $_hash;
public function __construct() {
$this->_hash = self::_getRandomString(15);
}
/**
*
* @param string $property
* @param callback $handler
* @return void
*/
public function addLazyEvalutionObserver($property, $handler) {
$className = get_class($this);
$hash = $this->_hash;
if (property_exists($this, $property)) {
if (is_callable($handler)) {
self::$_defaults[$hash][$property] = $this->{$property};
unset($this->{$property});
self::$_events[$hash][$property] = $handler;
} else {
if (is_array($handler))
$handler = sprintf('%s::%s', get_class($handler[0]), $handler[1]);
trigger_error(sprintf('Observer (function) does not exist: %s()', $handler), E_USER_WARNING);
}
} else
trigger_error(sprintf('Can\'t add observer at property: %s::$%s', $className, $property), E_USER_WARNING);
}
/**
*
* @param string $property
* @return mixed
*/
public function __get($property) {
$className = get_class($this);
$hash = $this->_hash;
if (array_key_exists($property, self::$_defaults[$hash])) {
$returnValue = null;
if (isset(self::$_events[$hash][$property])) {
$handler = self::$_events[$hash][$property];
$returnValue = call_user_func_array($handler, array($property, self::$_defaults[$hash][$property]));
}
if (!isset($this->{$property}) && $returnValue === self::DEFAULT_VALUE)
return self::$_defaults[$hash][$property];
else
return $returnValue;
} else {
trigger_error(sprintf('Undefined property: %s::$%s', $className, $property), E_USER_NOTICE);
}
}
private static function _getRandomString($length = 8){
$list = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
mt_srand();
$result = '';
for ($i = 0; $i < $length; $i++) $result .= $list{mt_rand(0, strlen($list) - 1)};
return $result;
}
}