Source for file SObject.class.php

Documentation is available at SObject.class.php

  1. <?php
  2. /**
  3. * @since 5/5/05
  4. * @package harmoni.primitives
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: SObject.class.php,v 1.4 2007/09/04 20:25:28 adamfranco Exp $
  10. *
  11. * @link http://harmoni.sourceforge.net/
  12. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  13. */
  14.  
  15. /**
  16. * SObject (Squeak/Smalltalk-like object).
  17. *
  18. * In Smalltalk, all object share a common class, "Object", which defines common
  19. * methods for all objects. This class holds a subset of those methods in Object
  20. * that are needed in this package.
  21. *
  22. * @since 5/5/05
  23. * @package harmoni.primitives
  24. *
  25. * @copyright Copyright &copy; 2005, Middlebury College
  26. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  27. *
  28. * @version $Id: SObject.class.php,v 1.4 2007/09/04 20:25:28 adamfranco Exp $
  29. *
  30. * @link http://harmoni.sourceforge.net/
  31. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  32. */
  33. class SObject {
  34.  
  35. /*******************************************************
  36. * Class Methods
  37. *********************************************************/
  38.  
  39. /**
  40. * Create an object that has similar contents to aSimilarObject.
  41. * If the classes have any instance varaibles with the same names, copy them across.
  42. * If this is bad for a class, override this method.
  43. *
  44. * @param string $targetClass As mentiond here,
  45. * {@link http://www.php.net/manual/en/ref.classobj.php} there is no good way
  46. * to inherit class methods such that they can know the class of
  47. * the reciever (child class) instead of the class name of the implementer
  48. * (parent class). As such, we need to pass our target classname.
  49. * @param object $aSimilarObject
  50. * @return object
  51. * @access public
  52. * @static
  53. * @since 5/5/05
  54. */
  55. function newFrom ( $targetClass, $aSimilarObject ) {
  56. $newObject = new $targetClass();
  57. $newObject->copySameFrom($aSimilarObject);
  58. return $newObject;
  59. }
  60. /*******************************************************
  61. * Instance Methods - Comparing
  62. *********************************************************/
  63.  
  64. /**
  65. * Answer whether the receiver and the argument are the same.
  66. * If = is redefined in any subclass, consider also redefining the
  67. * message hash.
  68. *
  69. * @param object $anObject
  70. * @return boolean
  71. * @access public
  72. * @since 7/11/05
  73. */
  74. function isEqualTo ( $anObject ) {
  75. return ($this === $anObject);
  76. }
  77. /**
  78. * Answer whether the receiver and the argument are the same.
  79. *
  80. * WARNING: This method is here for convience. DO NOT OVERRIDE.
  81. * OVERRIDE isEqualTo() instead.
  82. *
  83. * @param object $anObject
  84. * @return boolean
  85. * @access public
  86. * @since 7/11/05
  87. */
  88. function isEqual ( $anObject ) {
  89. return $this->isEqualTo($anObject);
  90. }
  91. /**
  92. * Answer whether the receiver and the argument are not the
  93. * same.
  94. *
  95. * @param object $anObject
  96. * @return boolean
  97. * @access public
  98. * @since 7/11/05
  99. */
  100. function isNotEqualTo ( $anObject ) {
  101. return !($this->isEqualTo($anObject));
  102. }
  103. /**
  104. * Answer whether the receiver and the argument Reference the same object.
  105. *
  106. * @param object $anObject
  107. * @return boolean
  108. * @access public
  109. * @since 7/11/05
  110. */
  111. function isReferenceTo ( $anObject ) {
  112. // Store the value of $anObject
  113. $temp = $anObject;
  114. // Set the value of $anObject to something unique and see if $this
  115. // has changed as well.
  116. $anObject = uniqid("test_ref");
  117. $is_ref = ($anObject === $this);
  118. // Put back the original value.
  119. $anObject = $temp;
  120. return $is_ref;
  121. }
  122. /**
  123. * Answer whether the receiver and the argument do not reference the same object.
  124. *
  125. * @param object $anObject
  126. * @return boolean
  127. * @access public
  128. * @since 7/11/05
  129. */
  130. function isNotReferenceTo ( $anObject ) {
  131. return !($this->isReferenceTo($anObject));
  132. }
  133. /*******************************************************
  134. * Instance Methods - Converting
  135. *********************************************************/
  136.  
  137. /**
  138. * Create an object of class aSimilarClass that has similar contents to the
  139. * receiver.
  140. *
  141. * 'as' seems to be a reserved word, so 'asA' is used instead.
  142. *
  143. * @param string $aSimilarClass
  144. * @return object
  145. * @access public
  146. * @since 5/5/05
  147. */
  148. function asA ( $aSimilarClass ) {
  149. $obj = SObject::newFrom($aSimilarClass, $this);
  150. return $obj;
  151. }
  152. /**
  153. * Answer a String whose characters are a description of the receiver.
  154. * To change behavior, override printableString(), not this method.
  155. *
  156. * @return string
  157. * @access public
  158. * @since 7/11/05
  159. */
  160. function asString () {
  161. return $this->printableString();
  162. }
  163. /**
  164. * Answer a String whose characters are a description of the receiver.
  165. * Override this method as needed to provide a better representation
  166. *
  167. * @return string
  168. * @access public
  169. * @since 7/11/05
  170. */
  171. function printableString () {
  172. $classname = get_class($this);
  173. $string = 'a';
  174. if (in_array(strtolower($classname[0]), array('a', 'e', 'i', 'o', 'u')))
  175. $string .= 'n';
  176. $classname[0] = strtoupper($classname[0]);
  177. $string .= ' '.$classname;
  178. return $string;
  179. }
  180. /*******************************************************
  181. * Instance Methods - Copying
  182. *********************************************************/
  183.  
  184. /**
  185. * Answer another instance just like the receiver. Subclasses typically
  186. * override postCopy; they typically do not override shallowCopy.
  187. *
  188. * @return object
  189. * @access public
  190. * @since 7/11/05
  191. */
  192. function copy () {
  193. $newObject =$this->shallowCopy();
  194. return $newObject->postCopy();
  195. }
  196. /**
  197. * Copy to myself all instance variables named the same in otherObject.
  198. * This ignores otherObject's control over its own inst vars.
  199. *
  200. * @param object $otherObject
  201. * @return void
  202. * @access public
  203. * @since 5/5/05
  204. */
  205. function copySameFrom ( $otherObject ) {
  206. $myVars = get_object_vars($this);
  207. $otherVars = get_object_vars($otherObject);
  208. foreach (array_keys($myVars) as $varName) {
  209. if (key_exists($varName, $otherVars))
  210. $this->$varName = $otherVars[$varName];
  211. }
  212. }
  213. /**
  214. * one more level than a shallowCopy
  215. *
  216. * @return object
  217. * @access public
  218. * @since 7/11/05
  219. */
  220. function copyTwoLevel () {
  221. $class = get_class($this);
  222. $newObject = new $class;
  223. $varList = array_keys(get_object_vars($this));
  224. foreach ($varList as $varName) {
  225. // Use shallow-copy if we can
  226. if (is_object($this->$varName)
  227. && method_exists($this->$varName, 'shallowCopy'))
  228. {
  229. $newObject->$varName =$this->$varName->shallowCopy();
  230. }
  231. // Otherwise use PHP's copy-by-value
  232. else {
  233. $newObject->$varName = $this->$varName;
  234. }
  235. }
  236. return $newObject;
  237. }
  238. /**
  239. * Answer a copy of the receiver with its own copy of each instance
  240. * variable.
  241. *
  242. * @return object
  243. * @access public
  244. * @since 7/11/05
  245. */
  246. function deepCopy () {
  247. $class = get_class($this);
  248. $newObject = new $class;
  249. $varList = array_keys(get_object_vars($this));
  250. foreach ($varList as $varName) {
  251. // Use deep-copy if we can
  252. if (is_object($this->$varName)
  253. && method_exists($this->$varName, 'deepCopy'))
  254. {
  255. $newObject->$varName =$this->$varName->deepCopy();
  256. }
  257. // If it is an Array, copy the values
  258. else if (is_array($this->$varName)) {
  259. $newObject->$varName = SObject::_deepCopyArray($this->$varName);
  260. }
  261. // Otherwise use PHP's copy-by-value
  262. else {
  263. $newObject->$varName = $this->$varName;
  264. }
  265. }
  266. return $newObject;
  267. }
  268. /**
  269. * Recursively copy an array, used by deepCopy
  270. *
  271. * @param array, the input array.
  272. * @return array, a deep copy of the input array
  273. * @access private
  274. * @since 7/12/05
  275. * @static
  276. */
  277. function _deepCopyArray ( $array ) {
  278. $newArray = array();
  279. foreach (array_keys($array) as $key) {
  280. // Use deep-copy if we can on Objects
  281. if (is_object($array[$key])
  282. && method_exists($array[$key], 'deepCopy'))
  283. {
  284. $newArray[$key] =$array[$key]->deepCopy();
  285. }
  286. // If it is an Array, copy the values
  287. else if (is_array($array[$key])) {
  288. $newArray[$key] = SObject::_deepCopyArray($array[$key]);
  289. }
  290. // Otherwise use PHP's copy-by-value
  291. else {
  292. $newArray[$key] = $array[$key];
  293. }
  294. }
  295. return $newArray;
  296. }
  297. /**
  298. * $this is a shallow copy, subclasses should override to copy fields as
  299. * necessary to complete the full copy.
  300. *
  301. * @return object
  302. * @access public
  303. * @since 7/11/05
  304. */
  305. function postCopy () {
  306. /* override to copy fields as necessary to complete the full copy. */
  307. return $this;
  308. }
  309. /**
  310. * Answer a copy of the receiver which shares the receiver's instance variables.
  311. *
  312. * @return object
  313. * @access public
  314. * @since 7/11/05
  315. */
  316. function shallowCopy () {
  317. $class = get_class($this);
  318. $newObject = new $class;
  319. $varList = array_keys(get_object_vars($this));
  320. foreach ($varList as $varName) {
  321. if (is_object($this->$varName))
  322. $newObject->$varName =$this->$varName;
  323. else
  324. $newObject->$varName = $this->$varName;
  325. }
  326. return $newObject;
  327. }
  328. }
  329.  
  330. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:36 -0400 by phpDocumentor 1.3.0RC3