PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/Object.php

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