tags:

views:

414

answers:

2

Edit: This behaviour is reproducible with query globals on.

I have the following:

  $_SESSION['query_key'] = $_GET['query_key'];
  print($query_key);

Vs.

  $_SESSION['query_key'] = clone $_GET['query_key'];
  print($query_key);

The former prints out the value of $query_key, while the latter prints nothing. What sort of weird side effect is this of clone?

+3  A: 

You must be doing something very weird with your code. clone is for use on objects. Unless you're stuffing objects into $_GET then that code will result in a fatal error (or a warning in older PHP versions).

@Michael Haren - clone actually does a shallow copy of an object, that is, it copies all the properties, but if a property is a reference to another object it will copy the reference, not clone the other object.

Greg
See http://stackoverflow.com/questions/301766/php5-get-variables for why I'm using clone, thanks.
EoghanM
A: 

I know this doesn't really answer the question specifically, but based on your comment to Roborg I don't think this is a good solution for the problem you refer to in your other question (here) - you would be better off disabling register_globals

as soon as you do

$_SESSION['query_key'] = 'anything'

$query_key will be a reference to $_SESSION['query_key'], so cloning what you are putting into it will make no difference

Edit

Cloning only works with objects, so you cannot clone a string. That will result in a fatal error. I think if you look in your logs or set display_errors to 'On' you will get an error rather than a blank page

Tom Haigh
Thanks tomaigh, disabling register_globals fixed it... your answer hasn't explained why it was blank after the clone however.. any idea?
EoghanM