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