Created
October 2, 2012 15:39
-
-
Save kler/3820241 to your computer and use it in GitHub Desktop.
Circular references may cause segmentation fault
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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