compatibility.php 5.3KB

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