Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Sep 062010

Clone an object in PHP


It is easy to clone an object in PHP, using the following method.


    $obj = new stdClass(); 
    $obj->foo = "foo";
    $obj->bar = "bar";

    $new_obj = clone $obj;
    var_dump ( new_obj );

It will print out...

object(stdClass)#2 (2) {
  string(3) "foo"
  string(3) "bar"

 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>