1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
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: * and optional 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', create_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: */
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: /**
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 NObjectMixin::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: $class = get_called_class();
94: throw new MemberAccessException("Call to undefined static method $class::$name().");
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 = get_called_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: throw new MemberAccessException("Cannot unset the property {$this->reflection->name}::\$$name.");
170: }
171:
172: }
173: