PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/core/app/Object.php

http://zoop.googlecode.com/
PHP | 142 lines | 134 code | 8 blank | 0 comment | 5 complexity | 04ec376fb434ac22ab8c263de35a2c9e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. class Object
  3. {
  4. private $mixins = array();
  5. private $getters = array();
  6. private $setters = array();
  7. private $mixinOwner;
  8. protected $allowAdhocAttributes = true;
  9. private $adhocAttributes = array();
  10. protected function addGetter($name)
  11. {
  12. $this->getters[$name] = $name;
  13. }
  14. protected function addSetter($name)
  15. {
  16. $this->setters[$name] = $name;
  17. }
  18. protected function addGetSetter($name)
  19. {
  20. $this->addGetter($name);
  21. $this->addSetter($name);
  22. }
  23. public function __isset($name)
  24. {
  25. if(isset($this->getters[$name]))
  26. return true;
  27. if($this->mixinOwner && property_exists($this->mixinOwner, $name))
  28. return true;
  29. if($this->allowAdhocAttributes && isset($this->adhocAttributes[$name]))
  30. return true;
  31. return false;
  32. }
  33. public function __unset($name)
  34. {
  35. trigger_error("I'm not even sure what this should do yet? Do getters and setters count here or just adhoc attributes");
  36. }
  37. public function &__get($name)
  38. {
  39. if(isset($this->getters[$name]))
  40. {
  41. $funcName = "get$name";
  42. return $this->$funcName();
  43. }
  44. if($this->mixinOwner && property_exists($this->mixinOwner, $name))
  45. return $this->mixinOwner->$name;
  46. if($this->allowAdhocAttributes && isset($this->adhocAttributes[$name]))
  47. return $this->adhocAttributes[$name];
  48. else
  49. trigger_error("attributes $name does not exist");
  50. }
  51. public function __set($name, $value)
  52. {
  53. if(isset($this->setters[$name]))
  54. {
  55. $funcName = "set$Name";
  56. $this->$funcName($value);
  57. return;
  58. }
  59. if($this->mixinOwner && property_exists($this->mixinOwner, $name))
  60. return $this->mixinOwner->$name = $value;
  61. if(isset($this->getters[$name]))
  62. trigger_error("attributes $name is read only");
  63. else
  64. {
  65. if($this->allowAdhocAttributes)
  66. $this->adhocAttributes[$name] = $value;
  67. else
  68. trigger_error("attributes $name does not exist");
  69. }
  70. }
  71. protected function mixin($className, $params = NULL)
  72. {
  73. if(isset($this->mixins[$className]))
  74. return;
  75. $this->mixins[$className] = new $className();
  76. $this->mixins[$className]->mixedInto($this);
  77. if(method_exists($this->mixins[$className], 'init'))
  78. $this->mixins[$className]->init($params);
  79. return $this->mixins[$className];
  80. }
  81. protected function mixedInto($mixer)
  82. {
  83. $this->mixinOwner = $mixer;
  84. $this->mixins[get_class($mixer)] = $mixer;
  85. }
  86. protected function getMixinOwner()
  87. {
  88. return $this->mixinOwner;
  89. }
  90. public function _methodExists($methodName)
  91. {
  92. if(method_exists($this, $methodName))
  93. return true;
  94. foreach($this->mixins as $className => $thisMixin)
  95. {
  96. $class = new ReflectionClass($className);
  97. if($class->hasMethod($methodName))
  98. return true;
  99. }
  100. return false;
  101. }
  102. public function __call($methodName, $args)
  103. {
  104. // $t = microtime(1);
  105. // foreach($this->mixins as $className => $thisMixin)
  106. // echo get_class($this) . " $className $methodName $t<br>";
  107. foreach($this->mixins as $className => $thisMixin)
  108. {
  109. // what is faster, ReflectionClass or method_exists
  110. $class = new ReflectionClass($className);
  111. if($class->hasMethod($methodName))
  112. {
  113. return call_user_func_array(array($thisMixin, $methodName), $args);
  114. }
  115. }
  116. trigger_error("Method '$methodName' not found");
  117. }
  118. }