Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeInterface::clone() should not use unserialize(serialize()). #6

Closed
drupol opened this issue Jun 14, 2019 · 1 comment
Closed

NodeInterface::clone() should not use unserialize(serialize()). #6

drupol opened this issue Jun 14, 2019 · 1 comment

Comments

@drupol
Copy link
Collaborator

drupol commented Jun 14, 2019

The current NodeInterface::clone() method uses unserialize() and serialize().

This is not the best solution to clone an object, ideally we should have used __clone().

I've tests many solutions and the only one that works is with unserialize(serialize()).

You can easily test your solution using this code:

<?php

declare(strict_types = 1);

use Graphp\GraphViz\GraphViz;

include './vendor/autoload.php';

$exporterGraph = new \drupol\phptree\Exporter\Graph();

$root = new \drupol\phptree\Node\ValueNode('root', 2);
$root[] = new \drupol\phptree\Node\ValueNode('foo');

$clone = $root->clone();

echo 'Root count: ' . iterator_count($root->all()) . "\n";
echo 'Second clone count: ' . iterator_count($clone->all()) . "\n";

var_dump($root === $root[0]->getParent());
var_dump(sha1(spl_object_hash($root)) . ' === ' . sha1(spl_object_hash($root[0]->getParent())));
(new GraphViz())->setFormat('svg')->display($exporterGraph->export($root));

var_dump($clone === $clone[0]->getParent());
var_dump(sha1(spl_object_hash($clone)) . ' === ' .sha1(spl_object_hash($clone[0]->getParent())));
(new GraphViz())->setFormat('svg')->display($exporterGraph->export($clone));
@drupol
Copy link
Collaborator Author

drupol commented Jun 14, 2019

Fixed.

@drupol drupol closed this as completed Jun 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant