user478280
user478280

Reputation:

Can't clone an object in php simple dom parser

First things first, I'd like to make sure the below is the right way to test and clone an object. I'm using the PHP Simple HTML DOM Parser.

$page = 'www.google.ca';
$html = file_get_html($page);

$test = clone $html;

$test->find('title',0)->innertext = 'changed title';

echo $html->find('title',0)->innertext;
echo $test->find('title',0)->innertext;

Now maybe I'm doing it wrong, but this doesn't seem to clone $html to $test. Both will output 'changed title'.

So my question(s) is,

  1. Am I cloning correctly?
  2. Is there another way of cloning an object in PHP?

Thanks guys

Upvotes: 0

Views: 574

Answers (1)

adrian7
adrian7

Reputation: 1016

The documantation says so:

When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references.

So maybe .innertext refers to another object, and it's a reference.

Have you tried $test = $html;?

Upvotes: 1

Related Questions