How to clone a string?

Thread Solved

Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

How to clone a string?

 
0
  #1
Nov 9th, 2008
I have a reference to some string. I need to copy (clone) that string.
This is what I tried
  1. $newString = $refToString; //this just copies the ref - no new memory allocated
  2. $newString = clone $refToString; //this creates "__clone method called on non-object" error
If anyone knows how to do this, please help!
Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to clone a string?

 
0
  #2
Nov 9th, 2008
Originally Posted by edek View Post
I have a reference to some string. I need to copy (clone) that string.
This is what I tried
  1. $newString = $refToString; //this just copies the ref - no new memory allocated
  2. $newString = clone $refToString; //this creates "__clone method called on non-object" error
If anyone knows how to do this, please help!
Thanks
I believe strings are always copied/cloned on either assignment or modification.

Try:

  1. $str = "joe";
  2. $str2 = $str;
  3. $str2 .= " blo"; // you have cloned $str to $str2 by modifying $str2
  4.  
  5. echo $str; // would give you: joe

I'm sure this holds for all PHP versions to date since strings are "basic" types.

Objects are referenced however in PHP5 and above. PHP4 Objects are referenced on assignment. But cloned on modification.
Last edited by digital-ether; Nov 9th, 2008 at 9:29 pm.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

Re: How to clone a string?

 
0
  #3
Nov 10th, 2008
Yes, it works like it should. I just left it for a while, and when I got back It just worked like it should. I'm also unable to reproduce the problem.
\Thanks for your reply anyway!
Last edited by edek; Nov 10th, 2008 at 8:31 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 189
Reputation: martin5211 is an unknown quantity at this point 
Solved Threads: 14
martin5211 martin5211 is offline Offline
Junior Poster

Re: How to clone a string?

 
0
  #4
Nov 10th, 2008
I think clone is only used to clone object references on PHP 5. For some reason $refToString contained a string or null value result, that's why PHP indicated that error.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC