PHPのクラスで循環参照、 当然エラー
二つのクラスがお互いを循環参照すると、当然エラーなんだけれども、なんとなく確認したくなったので書いてみる。
<?php class ParentClass { protected $_child; public function __construct() { $this->_child = new ChildClass($this); } public function __call($name,$args) { return call_user_func(array($this->_child,$name),$args); } } class ChildClass { protected $_parent; public function __construct(ParentClass $parent) { $this->_parent = $parent; } public function __call($name,$args) { return call_user_func(array($this->_parent,$name),$args); } } $class = new ParentClass(); $class->hoge();
コマンドラインで実行すると
「セグメンテーション違反です」
処理中断
ああ、やっぱり。
Fatal Error ですらないのか。
__call() の中に method_exists() とか埋めて自分で処理。