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