Source for file Object.php

Documentation is available at Object.php

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