Source for file compat.php

Documentation is available at compat.php

  1. <?php
  2. /**
  3. * Compatability with various different PHP versions
  4. *
  5. * PHP5 changes the way the world revolves... well, it changes the way
  6. * objects are handled so that $object is always a reference to an
  7. * object. The consequence is that in PHP5, this:
  8. *     $object = new Foo();
  9. *     $copy = $object;
  10. * is equivalent to
  11. *     $object = new Foo();
  12. *     $copy = &$object;
  13. * in PHP4. In PHP5, the = operator is copy by reference not copy by value.
  14. *
  15. * Conveniently, PHP5 provides you with the clone keyword to get around this.
  16. * Inconveniently, that same keyword is not in PHP4 so you can't just
  17. * use it to get around your problems.
  18. *
  19. * If the version of PHP being used here is earlier than 5.0, then use
  20. * a PHP-only inplementation of the clone keyword (as a function) from the
  21. * PEAR PHP_Compat library:
  22. *
  23. *            http://pear.php.net/package/PHP_Compat
  24. *
  25. * So now, instead of using the above construct to copy the $object, you
  26. * use:
  27. *     $object = new Foo();
  28. *     $copy = clone($object);
  29. *
  30. * Note that in normal PHP5, you would not include the parentheses as clone is
  31. * a keyword not a function, but for the PHP4 compatability to work, it has to be
  32. * written as a function. PHP5 will just throw away the parentheses, pretending
  33. * that they are for (an unneeded) grouping not a function call.
  34. *
  35. @author    Stuart Prescott
  36. @copyright  Copyright Stuart Prescott
  37. @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  38. @version    $Id$
  39. @package    Bumblebee
  40. @subpackage Misc
  41. */
  42.  
  43. /** Load ancillary functions */
  44. require_once 'inc/typeinfo.php';
  45.  
  46. if (version_compare(phpversion()'5.0'=== -1{
  47.   /** php-compat implementation of clone for PHP4 */
  48.   #include_once 'PHP/Compat/Function/clone.php';
  49.     // Needs to be wrapped in eval as clone is a keyword in PHP5
  50.         eval('
  51.       function clone($object) {
  52.           // Sanity check
  53.           if (!is_object($object)) {
  54.               user_error(\'clone() __clone method called on non-object\', E_USER_WARNING);
  55.               return;
  56.           }
  57.  
  58.           // Use serialize/unserialize trick to deep copy the object
  59.           #this results in a memory leak under PHP4.4.2
  60.           #$object = unserialize(serialize($object));
  61.  
  62.           // If there is a __clone method call it on the "new" class
  63.           if (method_exists($object, \'__clone\')) {
  64.               $object->__clone();
  65.           }
  66.  
  67.           return $object;
  68.       }
  69.  
  70.       function type_is_a($a, $b) {
  71.         return is_a($a, $b);
  72.       }
  73.  
  74.       function date_default_timezone_set($tz) {
  75.         putenv("TZ=$tz");
  76.       }
  77.  
  78.   ');
  79.  
  80. else {
  81.  
  82.   // PHP5 complains if you use the is_a construct, so may this function across to
  83.   // instanceof. Note that this is not exactly the same as instanceof will throw
  84.   // errors in places where is_a didn't.
  85.     eval('
  86.      function type_is_a($a, $b) {
  87.         return $a instanceof $b;
  88.      }
  89.   ');
  90.  
  91. }
  92.  
  93. ?>

Documentation generated on Tue, 06 Mar 2007 10:01:05 +0000 by phpDocumentor 1.3.0