compatibility.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @version $Id: compatibility.php 1723 2008-04-08 00:34:10Z lastcraft $
  6. */
  7. /**
  8. * Static methods for compatibility between different
  9. * PHP versions.
  10. * @package SimpleTest
  11. */
  12. class SimpleTestCompatibility {
  13. /**
  14. * Creates a copy whether in PHP5 or PHP4.
  15. * @param object $object Thing to copy.
  16. * @return object A copy.
  17. * @access public
  18. * @static
  19. */
  20. function copy($object) {
  21. if (version_compare(phpversion(), '5') >= 0) {
  22. $copy = null;
  23. eval('$copy = clone $object;');
  24. return $copy;
  25. }
  26. return $object;
  27. }
  28. /**
  29. * Identity test. Drops back to equality + types for PHP5
  30. * objects as the === operator counts as the
  31. * stronger reference constraint.
  32. * @param mixed $first Test subject.
  33. * @param mixed $second Comparison object.
  34. * @return boolean True if identical.
  35. * @access public
  36. * @static
  37. */
  38. function isIdentical($first, $second) {
  39. if (version_compare(phpversion(), '5') >= 0) {
  40. return SimpleTestCompatibility::_isIdenticalType($first, $second);
  41. }
  42. if ($first != $second) {
  43. return false;
  44. }
  45. return ($first === $second);
  46. }
  47. /**
  48. * Recursive type test.
  49. * @param mixed $first Test subject.
  50. * @param mixed $second Comparison object.
  51. * @return boolean True if same type.
  52. * @access private
  53. * @static
  54. */
  55. function _isIdenticalType($first, $second) {
  56. if (gettype($first) != gettype($second)) {
  57. return false;
  58. }
  59. if (is_object($first) && is_object($second)) {
  60. if (get_class($first) != get_class($second)) {
  61. return false;
  62. }
  63. return SimpleTestCompatibility::_isArrayOfIdenticalTypes(
  64. get_object_vars($first),
  65. get_object_vars($second));
  66. }
  67. if (is_array($first) && is_array($second)) {
  68. return SimpleTestCompatibility::_isArrayOfIdenticalTypes($first, $second);
  69. }
  70. if ($first !== $second) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. /**
  76. * Recursive type test for each element of an array.
  77. * @param mixed $first Test subject.
  78. * @param mixed $second Comparison object.
  79. * @return boolean True if identical.
  80. * @access private
  81. * @static
  82. */
  83. function _isArrayOfIdenticalTypes($first, $second) {
  84. if (array_keys($first) != array_keys($second)) {
  85. return false;
  86. }
  87. foreach (array_keys($first) as $key) {
  88. $is_identical = SimpleTestCompatibility::_isIdenticalType(
  89. $first[$key],
  90. $second[$key]);
  91. if (! $is_identical) {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. /**
  98. * Test for two variables being aliases.
  99. * @param mixed $first Test subject.
  100. * @param mixed $second Comparison object.
  101. * @return boolean True if same.
  102. * @access public
  103. * @static
  104. */
  105. function isReference(&$first, &$second) {
  106. if (version_compare(phpversion(), '5', '>=') && is_object($first)) {
  107. return ($first === $second);
  108. }
  109. if (is_object($first) && is_object($second)) {
  110. $id = uniqid("test");
  111. $first->$id = true;
  112. $is_ref = isset($second->$id);
  113. unset($first->$id);
  114. return $is_ref;
  115. }
  116. $temp = $first;
  117. $first = uniqid("test");
  118. $is_ref = ($first === $second);
  119. $first = $temp;
  120. return $is_ref;
  121. }
  122. /**
  123. * Test to see if an object is a member of a
  124. * class hiearchy.
  125. * @param object $object Object to test.
  126. * @param string $class Root name of hiearchy.
  127. * @return boolean True if class in hiearchy.
  128. * @access public
  129. * @static
  130. */
  131. function isA($object, $class) {
  132. if (version_compare(phpversion(), '5') >= 0) {
  133. if (! class_exists($class, false)) {
  134. if (function_exists('interface_exists')) {
  135. if (! interface_exists($class, false)) {
  136. return false;
  137. }
  138. }
  139. }
  140. $is_a = null;
  141. eval("\$is_a = \$object instanceof $class;");
  142. return $is_a;
  143. }
  144. if (function_exists('is_a')) {
  145. return is_a($object, $class);
  146. }
  147. return ((strtolower($class) == get_class($object))
  148. or (is_subclass_of($object, $class)));
  149. }
  150. /**
  151. * Sets a socket timeout for each chunk.
  152. * @param resource $handle Socket handle.
  153. * @param integer $timeout Limit in seconds.
  154. * @access public
  155. * @static
  156. */
  157. function setTimeout($handle, $timeout) {
  158. if (function_exists('stream_set_timeout')) {
  159. stream_set_timeout($handle, $timeout, 0);
  160. } elseif (function_exists('socket_set_timeout')) {
  161. socket_set_timeout($handle, $timeout, 0);
  162. } elseif (function_exists('set_socket_timeout')) {
  163. set_socket_timeout($handle, $timeout, 0);
  164. }
  165. }
  166. }
  167. ?>