Author Topic: OO Programming in PHP - Copying and Cloning (Page 6 of 7 )  (Read 149 times)

0 Members and 1 Guest are viewing this topic.

Offline JusticeLeague

  • Sr. Member
  • *
  • Posts: 352
  • Karma: +1/-0
    • View Profile
OO Programming in PHP - Copying and Cloning (Page 6 of 7 )
« on: August 31, 2008, 11:13:50 PM »
Object Oriented Programming in PHP - Copying and Cloning

(Page 6 of 7 )

When you create an object $obj you can copy the object by doing $obj2=$obj, the new object is a copy (not a reference) of $obj so it has the state $obj had in the moment the assignment was made.

Sometimes you don\'t want this you just want to create a new object of the same class as obj, calling the constructor of the new object as if you had used the new statement.

This can be done in PHP using serialization and a base class that all other classes must extend.

Entering a Danger Zone

When you serialize an object you get a string which has a certain format, you may investigate this if you are curious. One of the things the string has is the name of the class (nice!), you can extract it using:

<?php

$herring=serialize($obj);
$vec=explode(\':\',$herring);
$nam=str_replace(""",\'\',$vec[2]);

?>

So suppose you create a class "Universe" and force that all classes must extend universe, you can define a method clone in Universe as:

<?php

class Universe {
function clone() {
$herring=serialize($this);
$vec=explode(\':\',$herring);
$nam=str_replace(""",\'\',$vec[2]);
$ret=new $nam;
return $ret;
}
}

Then:

$obj=new Something();
//Something extends Universe !!
$other=$obj->clone();

?>

What you get is a new object of class Something created the same way as using new; the constructor is called, etc. I don\'t know if this is useful for you but the Universe class which knows the name of the derived class is a nice concept to experiment with. The only limit is your imagination.

Note: I\'m using PHP4, some of these examples may not work in PHP3.

Techronnati | where technology never sleeps

OO Programming in PHP - Copying and Cloning (Page 6 of 7 )
« on: August 31, 2008, 11:13:50 PM »

Mountain View

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
165 Views
Last post August 31, 2008, 11:06:03 PM
by JusticeLeague
1 Replies
214 Views
Last post October 13, 2014, 02:47:23 AM
by MoneyRepublic
0 Replies
183 Views
Last post August 31, 2008, 11:11:47 PM
by JusticeLeague
0 Replies
170 Views
Last post August 31, 2008, 11:20:10 PM
by JusticeLeague
0 Replies
31 Views
Last post October 20, 2016, 12:57:49 AM
by Corps

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.