Dismiss Notice
Welcome to Our Community
Wanting to join the rest of our members? Feel free to sign up today.

PHP Tricks - PHP 5 clone in PHP4

Discussion in 'General Webmaster Helpdesk' started by ovi, Jun 24, 2005.

  1. ovi

    ovi Guest

    PHP 5 handles object differently from PHP4

    In PHP4
    $new_object = $object;
    will copy $object to $new_object

    In PHP5
    The $new_object will point to $ojbect, kind of like Java.

    The correct syntax in PHP5 should be:
    $new_object = clone($object);

    It's all good but create lots of confusion.
    To make the two compatible, here's a clone function that works for both PHP 4 and PHP5:

    /* ************************************** Backward compatible
     */ function php4_clone($object) {
      if (version_compare(phpversion(), '5.0') < 0) {
       return $object;
      } else {
       return @clone($object);
    To use the function: $object=php4_clone($old_object); It works on both PHP4 and PHP5


Featured Resources (View All)

Share This Page