|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00013 class SimpleTestCompatibility { 00014 00022 function copy($object) { 00023 if (version_compare(phpversion(), '5') >= 0) { 00024 eval('$copy = clone $object;'); 00025 return $copy; 00026 } 00027 return $object; 00028 } 00029 00040 function isIdentical($first, $second) { 00041 if (version_compare(phpversion(), '5') >= 0) { 00042 return SimpleTestCompatibility::_isIdenticalType($first, $second); 00043 } 00044 if ($first != $second) { 00045 return false; 00046 } 00047 return ($first === $second); 00048 } 00049 00058 function _isIdenticalType($first, $second) { 00059 if (gettype($first) != gettype($second)) { 00060 return false; 00061 } 00062 if (is_object($first) && is_object($second)) { 00063 if (get_class($first) != get_class($second)) { 00064 return false; 00065 } 00066 return SimpleTestCompatibility::_isArrayOfIdenticalTypes( 00067 get_object_vars($first), 00068 get_object_vars($second)); 00069 } 00070 if (is_array($first) && is_array($second)) { 00071 return SimpleTestCompatibility::_isArrayOfIdenticalTypes($first, $second); 00072 } 00073 if ($first !== $second) { 00074 return false; 00075 } 00076 return true; 00077 } 00078 00087 function _isArrayOfIdenticalTypes($first, $second) { 00088 if (array_keys($first) != array_keys($second)) { 00089 return false; 00090 } 00091 foreach (array_keys($first) as $key) { 00092 $is_identical = SimpleTestCompatibility::_isIdenticalType( 00093 $first[$key], 00094 $second[$key]); 00095 if (! $is_identical) { 00096 return false; 00097 } 00098 } 00099 return true; 00100 } 00101 00110 function isReference(&$first, &$second) { 00111 if (version_compare(phpversion(), '5', '>=') && is_object($first)) { 00112 return ($first === $second); 00113 } 00114 if (is_object($first) && is_object($second)) { 00115 $id = uniqid("test"); 00116 $first->$id = true; 00117 $is_ref = isset($second->$id); 00118 unset($first->$id); 00119 return $is_ref; 00120 } 00121 $temp = $first; 00122 $first = uniqid("test"); 00123 $is_ref = ($first === $second); 00124 $first = $temp; 00125 return $is_ref; 00126 } 00127 00137 function isA($object, $class) { 00138 if (version_compare(phpversion(), '5') >= 0) { 00139 if (! class_exists($class, false)) { 00140 if (function_exists('interface_exists')) { 00141 if (! interface_exists($class, false)) { 00142 return false; 00143 } 00144 } 00145 } 00146 eval("\$is_a = \$object instanceof $class;"); 00147 return $is_a; 00148 } 00149 if (function_exists('is_a')) { 00150 return is_a($object, $class); 00151 } 00152 return ((strtolower($class) == get_class($object)) 00153 or (is_subclass_of($object, $class))); 00154 } 00155 00163 function setTimeout($handle, $timeout) { 00164 if (function_exists('stream_set_timeout')) { 00165 stream_set_timeout($handle, $timeout, 0); 00166 } elseif (function_exists('socket_set_timeout')) { 00167 socket_set_timeout($handle, $timeout, 0); 00168 } elseif (function_exists('set_socket_timeout')) { 00169 set_socket_timeout($handle, $timeout, 0); 00170 } 00171 } 00172 } 00173 ?>