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