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