Skip to content

Instantly share code, notes, and snippets.

@kler
Created October 2, 2012 15:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kler/3820241 to your computer and use it in GitHub Desktop.
Save kler/3820241 to your computer and use it in GitHub Desktop.
Circular references may cause segmentation fault
<?php
// Conclusion: This code will cause Segmentation fault, because
// of recursive calls to __clone(), but PHP should handle this.
// Bug filed here: http://bugs.php.net/bug.php?id=49664
date_default_timezone_set('America/Los_Angeles');
class Test {
public $previous, $next = NULL;
public $name = NULL;
public function __construct($name) {
$this->name = $name;
}
public function __clone() {
$this->previous != NULL ? $this->previous = clone $this->previous : false;
$this->next != NULL ? $this->next = clone $this->next : false;
}
public function __toString() {
return $this->name . ' [' . ($this->previous != NULL ? '<' :
'-') . ' ' . ($this->next != NULL ? '>' : '-') . ']';
}
}
// Create some test objects
$a = new Test('a'); $b = new Test('b');
// Link them together
$a->next =& $b; $b->previous =& $a;
// Clone and print
echo "before cloning:\n" . $a . "\n" . $b . "\n";
$b = clone $a;
echo "These two should NOT look the same:\n" . $a . "\n" . $b . "\n";
// Will output:
// before cloning:
// a [- >]
// b [< -]
// Segmentation fault
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment