Source for file compatibility.php

Documentation is available at compatibility.php

  1. <?php
  2. /**
  3.  *  base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @version    $Id: compatibility.php 1900 2009-07-29 11:44:37Z lastcraft $
  6.  */
  7.  
  8. /**
  9.  *  Static methods for compatibility between different
  10.  *  PHP versions.
  11.  *  @package    SimpleTest
  12.  */
  13.  
  14.     /**
  15.      *    Creates a copy whether in PHP5 or PHP4.
  16.      *    @param object $object     Thing to copy.
  17.      *    @return object            copy.
  18.      *    @access public
  19.      */
  20.     static function copy($object{
  21.         if (version_compare(phpversion()'5'>= 0{
  22.             eval('$copy = clone $object;');
  23.             return $copy;
  24.         }
  25.         return $object;
  26.     }
  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.      */
  37.     static function isIdentical($first$second{
  38.         if (version_compare(phpversion()'5'>= 0{
  39.             return SimpleTestCompatibility::isIdenticalType($first$second);
  40.         }
  41.         if ($first != $second{
  42.             return false;
  43.         }
  44.         return ($first === $second);
  45.     }
  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.      */
  54.     protected static function isIdenticalType($first$second{
  55.         if (gettype($first!= gettype($second)) {
  56.             return false;
  57.         }
  58.         if (is_object($first&& is_object($second)) {
  59.             if (get_class($first!= get_class($second)) {
  60.                 return false;
  61.             }
  62.             return SimpleTestCompatibility::isArrayOfIdenticalTypes(
  63.                     (array) $first,
  64.                     (array) $second);
  65.         }
  66.         if (is_array($first&& is_array($second)) {
  67.             return SimpleTestCompatibility::isArrayOfIdenticalTypes($first$second);
  68.         }
  69.         if ($first !== $second{
  70.             return false;
  71.         }
  72.         return true;
  73.     }
  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.      */
  82.     protected static function isArrayOfIdenticalTypes($first$second{
  83.         if (array_keys($first!= array_keys($second)) {
  84.             return false;
  85.         }
  86.         foreach (array_keys($firstas $key{
  87.             $is_identical SimpleTestCompatibility::isIdenticalType(
  88.                     $first[$key],
  89.                     $second[$key]);
  90.             if ($is_identical{
  91.                 return false;
  92.             }
  93.         }
  94.         return true;
  95.     }
  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.      */
  104.     static function isReference(&$first&$second{
  105.         if (version_compare(phpversion()'5''>='&& is_object($first)) {
  106.             return ($first === $second);
  107.         }
  108.         if (is_object($first&& is_object($second)) {
  109.             $id uniqid("test");
  110.             $first->$id true;
  111.             $is_ref = isset($second->$id);
  112.             unset($first->$id);
  113.             return $is_ref;
  114.         }
  115.         $temp $first;
  116.         $first uniqid("test");
  117.         $is_ref ($first === $second);
  118.         $first $temp;
  119.         return $is_ref;
  120.     }
  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.      */
  130.     static function isA($object$class{
  131.         if (version_compare(phpversion()'5'>= 0{
  132.             if (class_exists($classfalse)) {
  133.                 if (function_exists('interface_exists')) {
  134.                     if (interface_exists($classfalse))  {
  135.                         return false;
  136.                     }
  137.                 }
  138.             }
  139.             eval("\$is_a = \$object instanceof $class;");
  140.             return $is_a;
  141.         }
  142.         if (function_exists('is_a')) {
  143.             return is_a($object$class);
  144.         }
  145.         return ((strtolower($class== get_class($object))
  146.                 or (is_subclass_of($object$class)));
  147.     }
  148.  
  149.     /**
  150.      *    Sets a socket timeout for each chunk.
  151.      *    @param resource $handle    Socket handle.
  152.      *    @param integer $timeout    Limit in seconds.
  153.      *    @access public
  154.      */
  155.     static function setTimeout($handle$timeout{
  156.         if (function_exists('stream_set_timeout')) {
  157.             stream_set_timeout($handle$timeout0);
  158.         elseif (function_exists('socket_set_timeout')) {
  159.             socket_set_timeout($handle$timeout0);
  160.         elseif (function_exists('set_socket_timeout')) {
  161.             set_socket_timeout($handle$timeout0);
  162.         }
  163.     }
  164. }
  165. ?>

Documentation generated on Thu, 01 Oct 2009 20:54:27 -0500 by phpDocumentor 1.4.2