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