-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.php
86 lines (73 loc) · 2.07 KB
/
Object.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
<?php
/**
* Class Object
*
* @property Object prototype
*/
class Object
{
/**
* @var Object
*/
public static $_currentObject_;
private $_prototype_ = null;
private $_properties_ = [];
function __construct($constructor = null, array $arguments = [])
{
if ($constructor instanceof Constructor) {
$this->_prototype_ = $constructor->prototype;
self::$_currentObject_ = $this;
call_user_func_array($constructor, $arguments);
} elseif ($constructor instanceof self) {
$this->_prototype_ = $constructor;
} elseif (is_callable($constructor)) {
self::$_currentObject_ = $this;
call_user_func_array($constructor, $arguments);
} elseif (null !== $constructor) {
throw new Exception("Constructor argument's type is invalid.");
}
}
/**
* @return null|Object
*/
public function getPrototype()
{
return $this->_prototype_;
}
/**
* @param null|Object $prototype
*/
public function setPrototype(Object $prototype = null)
{
$this->_prototype_ = $prototype;
}
public function __call($name, $arguments)
{
$function = $this->$name;
if (!is_callable($function)) {
throw new Exception(sprintf('Property "%s" is not a function.', $name));
}
self::$_currentObject_ = $this;
return call_user_func_array($function, $arguments);
}
public function __get($name)
{
if ('prototype' === $name) {
return $this->getPrototype();
}
if (isset($this->_properties_[$name])) {
return $this->_properties_[$name];
}
if (null !== $this->_prototype_) {
return $this->_prototype_->$name;
}
throw new Exception(sprintf('Property "%s" is undefined.', $name));
}
public function __set($name, $value)
{
if ('prototype' === $name) {
$this->setPrototype($value);
}
$this->_properties_[$name] = $value;
}
}