How does PHP5 cloning work?

Edit: This behavior is reproducible with global queries .

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 the value of $ query_key, while the latter prints nothing. What's the weird side effect of cloning?

0


source to share


2 answers


You must be doing something very strange with your code. clone is for use on objects. If you don't stuff objects into $ _GET, then this code will result in a fatal error (or warning in older PHP versions).



@Michael Haren - A clone actually makes a shallow copy of an object, that is, it copies all properties, but if a property is a reference to another object, it copies the reference rather than clones the other object.

+3


source


I know this doesn't actually answer the question specifically, but based on your comment on Roborg, I don't think this is a good solution to the problem you are talking about in your other question ( here ) - you'd better turn off 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 put into it won't matter

Edit

Cloning only works on objects, so you cannot clone a string. This 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 and not a blank page

0


source







All Articles