TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (2) Thread Tools Search this Thread Display Modes
Old 11-05-2007, 12:59 PM   2 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,258
Thanks: 90
Wildhoney is on a distinguished road
Using the Clone Construct to Clone an Object

Cloning an object is quite different than it was in PHP4. In PHP4 you simply assigned the object to a new object and it would clone it verbatim. With the release of PHP5, things got a little more complex but a lot more flexible. PHP5 offers the clone construct along with a magic method to control the cloning process.

The reason for cloning is quite simple, much like the process itself if I was to be truthfully honest. You would use cloning to reduce the amount of code. For instance, consider the following:

PHP Code:
$pObject1 = new Object();
$pObject1->setName('Adam');
$pObject1->setAddress('http://www.talkphp.com/');

$pObject2 = new Object();
$pObject2->setName('Karl');
$pObject2->setAddress('http://www.talkphp.com/'); 
Would everyone be in agreement with me on this if I said cloning would make life a little easier? All we are doing is modifying the name, whilst the address remains to be identical as the previous one. Suppose we do this instead:

PHP Code:
$pObject1 = new Object();
$pObject1->setName('Adam');
$pObject1->setAddress('http://www.talkphp.com/');

$pObject2 = clone $pObject1;
$pObject2->setName('Karl'); 
We've saved ourselves 1 line! Fantastic, you might say, but that's not the half of it! Well, perhaps around half. PHP5 also introduces the __clone magic method to give you a little control over the process as a whole. Suppose we want to change the name when the object is cloned to Karl, we could do it like the following, although this would only be useful if you wanted to set the name as Karl upon every clone:

PHP Code:
public function __clone()
{
    
$this->m_szName 'Karl';

Do you see what happened? No, nor did I because we have no output, but what the above code would achieve in theory is it sets the name to Karl and so when you clone the object, there's no need to call setName! And let's face it, anything that saves us a little time is well worth it in the short and long run.

Just a little mention of this before we say goodbye, in our clone call. Let me remind you:

PHP Code:
$pObject2 = clone $pObject1
We are cloning the object which was set from the following line:

PHP Code:
$pObject1 = new Object(); 
You do not clone the class itself because the class itself does not store the data for any new instances, or any attempts at a clone instance, whilst $pObject1 does.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-05-2007, 01:49 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Nice article, I find clone very handy when I'm using the "Template" pattern. It allows me to clone the template and only fill in the details that change.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 11-16-2007, 03:39 AM   #3 (permalink)
The Wanderer
 
Join Date: Nov 2007
Location: Mumbai, India
Posts: 24
Thanks: 0
sunilbhatia79 is on a distinguished road
Default Clone Tutorial

Hi Guys,

Check this __clone() tutorial that I have written.

http://www.sunilb.com/php/php-tutori...__clone-method

Please let me know if you have any questions or comments.

Sunil
__________________
Sunil Bhatia www.twitter.com/sunilbhatia79 - Follow me on Twitter
PHP5 Tutorials
Career Articles
sunilbhatia79 is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1383-using-clone-construct-clone-object.html
Posted By For Type Date
PHP Advanced PHP: Saving Yourself Time with the Clone Construct! Tutorial This thread Refback 12-26-2007 04:02 PM
PHP OOP Advanced PHP: Saving Yourself Time with the Clone Construct! Tutorial This thread Refback 12-22-2007 09:14 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:27 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design