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