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

Documentation generated on Sun, 04 May 2008 09:21:22 -0500 by phpDocumentor 1.3.0