1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Nette\Object is the ultimate ancestor of all instantiable classes.
20: *
21: * It defines some handful methods and enhances object core of PHP:
22: * - access to undeclared members throws exceptions
23: * - support for conventional properties with getters and setters
24: * - support for event raising functionality
25: * - ability to add new methods to class (extension methods)
26: *
27: * Properties is a syntactic sugar which allows access public getter and setter
28: * methods as normal object variables. A property is defined by a getter method
29: * or setter method (no setter method means read-only property).
30: * <code>
31: * $val = $obj->label; // equivalent to $val = $obj->getLabel();
32: * $obj->label = 'Nette'; // equivalent to $obj->setLabel('Nette');
33: * </code>
34: * Property names are case-sensitive, and they are written in the camelCaps
35: * or PascalCaps.
36: *
37: * Event functionality is provided by declaration of property named 'on{Something}'
38: * Multiple handlers are allowed.
39: * <code>
40: * public $onClick; // declaration in class
41: * $this->onClick[] = 'callback'; // attaching event handler
42: * if (!empty($this->onClick)) ... // are there any handlers?
43: * $this->onClick($sender, $arg); // raises the event with arguments
44: * </code>
45: *
46: * Adding method to class (i.e. to all instances) works similar to JavaScript
47: * prototype property. The syntax for adding a new method is:
48: * <code>
49: * MyClass::extensionMethod('newMethod', function(MyClass $obj, $arg, ...) { ... });
50: * $obj = new MyClass;
51: * $obj->newMethod($x);
52: * </code>
53: *
54: * @author David Grudl
55: *
56: * @property-read Nette\Reflection\ClassType $reflection
57: */
58: abstract class Object
59: {
60:
61: /**
62: * Access to reflection.
63: * @return Nette\Reflection\ClassType
64: */
65: public static function getReflection()
66: {
67: return new Reflection\ClassType(get_called_class());
68: }
69:
70:
71:
72: /**
73: * Call to undefined method.
74: * @param string method name
75: * @param array arguments
76: * @return mixed
77: * @throws MemberAccessException
78: */
79: public function __call($name, $args)
80: {
81: return ObjectMixin::call($this, $name, $args);
82: }
83:
84:
85:
86: /**
87: * Call to undefined static method.
88: * @param string method name (in lower case!)
89: * @param array arguments
90: * @return mixed
91: * @throws MemberAccessException
92: */
93: public static function __callStatic($name, $args)
94: {
95: return ObjectMixin::callStatic(get_called_class(), $name, $args);
96: }
97:
98:
99:
100: /**
101: * Adding method to class.
102: * @param string method name
103: * @param mixed callback or closure
104: * @return mixed
105: */
106: public static function extensionMethod($name, $callback = NULL)
107: {
108: if (strpos($name, '::') === FALSE) {
109: $class = get_called_class();
110: } else {
111: list($class, $name) = explode('::', $name);
112: }
113: $class = new Reflection\ClassType($class);
114: if ($callback === NULL) {
115: return $class->getExtensionMethod($name);
116: } else {
117: $class->setExtensionMethod($name, $callback);
118: }
119: }
120:
121:
122:
123: /**
124: * Returns property value. Do not call directly.
125: * @param string property name
126: * @return mixed property value
127: * @throws MemberAccessException if the property is not defined.
128: */
129: public function &__get($name)
130: {
131: return ObjectMixin::get($this, $name);
132: }
133:
134:
135:
136: /**
137: * Sets value of a property. Do not call directly.
138: * @param string property name
139: * @param mixed property value
140: * @return void
141: * @throws MemberAccessException if the property is not defined or is read-only
142: */
143: public function __set($name, $value)
144: {
145: return ObjectMixin::set($this, $name, $value);
146: }
147:
148:
149:
150: /**
151: * Is property defined?
152: * @param string property name
153: * @return bool
154: */
155: public function __isset($name)
156: {
157: return ObjectMixin::has($this, $name);
158: }
159:
160:
161:
162: /**
163: * Access to undeclared property.
164: * @param string property name
165: * @return void
166: * @throws MemberAccessException
167: */
168: public function __unset($name)
169: {
170: ObjectMixin::remove($this, $name);
171: }
172:
173: }
174: