1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 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 string $class
57: * @property-read Nette\Reflection\ClassType $reflection
58: */
59: abstract class Object
60: {
61:
62: /**
63: * Access to reflection.
64: * @return Nette\Reflection\ClassType
65: */
66: public static function getReflection()
67: {
68: return new Reflection\ClassType(get_called_class());
69: }
70:
71:
72:
73: /**
74: * Call to undefined method.
75: * @param string method name
76: * @param array arguments
77: * @return mixed
78: * @throws MemberAccessException
79: */
80: public function __call($name, $args)
81: {
82: return ObjectMixin::call($this, $name, $args);
83: }
84:
85:
86:
87: /**
88: * Call to undefined static method.
89: * @param string method name (in lower case!)
90: * @param array arguments
91: * @return mixed
92: * @throws MemberAccessException
93: */
94: public static function __callStatic($name, $args)
95: {
96: return ObjectMixin::callStatic(get_called_class(), $name, $args);
97: }
98:
99:
100:
101: /**
102: * Adding method to class.
103: * @param string method name
104: * @param mixed callback or closure
105: * @return mixed
106: */
107: public static function extensionMethod($name, $callback = NULL)
108: {
109: if (strpos($name, '::') === FALSE) {
110: $class = get_called_class();
111: } else {
112: list($class, $name) = explode('::', $name);
113: }
114: $class = new Reflection\ClassType($class);
115: if ($callback === NULL) {
116: return $class->getExtensionMethod($name);
117: } else {
118: $class->setExtensionMethod($name, $callback);
119: }
120: }
121:
122:
123:
124: /**
125: * Returns property value. Do not call directly.
126: * @param string property name
127: * @return mixed property value
128: * @throws MemberAccessException if the property is not defined.
129: */
130: public function &__get($name)
131: {
132: return ObjectMixin::get($this, $name);
133: }
134:
135:
136:
137: /**
138: * Sets value of a property. Do not call directly.
139: * @param string property name
140: * @param mixed property value
141: * @return void
142: * @throws MemberAccessException if the property is not defined or is read-only
143: */
144: public function __set($name, $value)
145: {
146: return ObjectMixin::set($this, $name, $value);
147: }
148:
149:
150:
151: /**
152: * Is property defined?
153: * @param string property name
154: * @return bool
155: */
156: public function __isset($name)
157: {
158: return ObjectMixin::has($this, $name);
159: }
160:
161:
162:
163: /**
164: * Access to undeclared property.
165: * @param string property name
166: * @return void
167: * @throws MemberAccessException
168: */
169: public function __unset($name)
170: {
171: ObjectMixin::remove($this, $name);
172: }
173:
174: }
175: