/branches/new-router/lib/AkObject.php

https://github.com/akelos/v1 · PHP · 193 lines · 68 code · 42 blank · 83 comment · 7 complexity · 322fdaaa5e386c4aab2f11ce381b1f61 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Akelos Framework - http://www.akelos.org |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
  7. // | Released under the GNU Lesser General Public License, see LICENSE.txt|
  8. // +----------------------------------------------------------------------+
  9. /**
  10. * @package ActiveSupport
  11. * @subpackage Compatibility
  12. * @author Bermi Ferrer <bermi a.t akelos c.om>
  13. * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  14. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  15. */
  16. if(!class_exists('AkObject')){
  17. /**
  18. * Allows for __construct and __destruct to be used in PHP4.
  19. *
  20. * A hack to support __construct() on PHP 4
  21. * Hint: descendant classes have no PHP4 class_name()
  22. * constructors, so this one gets called first and calls the
  23. * top-layer __construct() which (if present) should call
  24. * parent::__construct()
  25. *
  26. * @author Bermi Ferrer <bermi a.t akelos c.om>
  27. * @copyright Copyright (c) 2002-2005, Akelos Media, S.L. http://www.akelos.org
  28. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  29. */
  30. class AkObject
  31. {
  32. // ------ CLASS METHODS ------ //
  33. // ---- Public methods ---- //
  34. // {{{ AkObject()
  35. /**
  36. * A hack to support __construct() on PHP 4
  37. *
  38. * Hint: descendant classes have no PHP4 class_name()
  39. * constructors, so this one gets called first and calls the
  40. * top-layer __construct() which (if present) should call
  41. * parent::__construct()
  42. *
  43. * @access public
  44. * @return void
  45. */
  46. function AkObject()
  47. {
  48. static $_callback_called;
  49. Ak::profile('Instantiating '.get_class($this));
  50. $args = func_get_args();
  51. // register_shutdown_function(array(&$this, '__destruct'));
  52. ____ak_shutdown_function(&$this);
  53. call_user_func_array(array(&$this, '__construct'), $args);
  54. if(empty($_callback_called)){
  55. $_callback_called = true;
  56. register_shutdown_function('____ak_shutdown_function');
  57. }
  58. }
  59. // }}}
  60. // {{{ toString()
  61. /**
  62. * Object-to-string conversion
  63. *
  64. * Each class can override it as necessary
  65. *
  66. * @access public
  67. * @return string in this case returns this class name
  68. */
  69. function toString()
  70. {
  71. return get_class($this);
  72. }
  73. function __toString()
  74. {
  75. return $this->toString();
  76. }
  77. // }}}
  78. // ---- Protected methods ---- //
  79. // {{{ __construct()
  80. /**
  81. * Class constructor, overriden in descendant classes
  82. *
  83. * @access protected
  84. * @return void
  85. */
  86. function __construct()
  87. {
  88. }
  89. // }}}
  90. // {{{ __destruct()
  91. /**
  92. * Class destructor, overriden in descendant classes
  93. *
  94. * @access protected
  95. * @return void
  96. */
  97. function __destruct()
  98. {
  99. unset($this);
  100. }
  101. // }}}
  102. // {{{ __clone()
  103. /**
  104. * Clone class (Zend Engine 2 compatibility trick)
  105. */
  106. function __clone()
  107. {
  108. return $this;
  109. }
  110. // }}}
  111. function log($message, $type = '', $identifyer = '')
  112. {
  113. require_once 'Log.php';
  114. $ident = empty($ident) ? 'main' : $ident;
  115. $log = Log::singleton('file', AK_LOGS_DIR.DS.$ident.'.log',$ident);
  116. $log->log($type, $message);
  117. }
  118. /**
  119. * Unsets circular reference children that are not freed from memory
  120. * when calling unset() or when the parent object is garbage collected.
  121. *
  122. * @see http://paul-m-jones.com/?p=262
  123. * @see http://bugs.php.net/bug.php?id=33595
  124. */
  125. function freeMemory()
  126. {
  127. // We can't use get_class_vars as it does not include runtime assigned attributes
  128. foreach (array_keys((array)$this) as $attribute){
  129. if(isset($this->$attribute)){
  130. unset($this->$attribute);
  131. }
  132. }
  133. }
  134. }
  135. function ____ak_shutdown_function($details = false)
  136. {
  137. static $___registered_objects;
  138. if(!$details){
  139. Ak::profile('Calling shutdown destructors');
  140. foreach (array_keys($___registered_objects) as $k){
  141. if(!empty($___registered_objects[$k]) && is_object($___registered_objects[$k]) && method_exists($___registered_objects[$k],'__destruct')){
  142. Ak::profile('Calling destructor for '.get_class($___registered_objects[$k]));
  143. $___registered_objects[$k]->__destruct();
  144. }
  145. }
  146. }else{
  147. $___registered_objects[] =& $details;
  148. }
  149. }
  150. }
  151. ?>