1

致命错误:达到“100”的最大函数嵌套级别,正在中止!在第 4 行的 C:\wamp\www\int\system\core\Controller.php

Call Stack
#   Time    Memory  Function    Location
1   0.0004  364608  {main}( )   ..\index.php:0
2   1.0350  433152  Bootstrap->__construct( )   ..\index.php:11
3   1.0355  438536  Welcome->__construct( ) ..\Bootstrap.php:7
4   1.0355  438536  Controller->__construct( )  ..\welcome.php:4
5   1.0356  438680  View->__construct( )    ..\Controller.php:4
6   1.0356  438680  Controller->__construct( )  ..\View.php:4

错误行:

<?php
class Controller {
    function __construct() {
        $this->view = new View(); // Error starts here
        $this->model = new Model();
        $this->tpl = new Template();
        $this->input = new Input();
        $this->lib = new Library();
        $this->session = new Session();
    }
}
?>

我将如何解决这个问题?我尝试扩展最大嵌套级别,但每次我将其增加到 200 时,它都会说致命错误达到了 200 的最大级别,正在中止!

更新:已修复:)

public function __construct() {
        self::$instance =& $this;
        $this->view = new View;
        $this->model = new Model;
        $this->tpl = new Template;
        $this->input = new Input;
        $this->lib = new Library;
        $this->session = new Session;
    }
    public static function &get_instance() {
        return self::$instance;
    }

在模型中:

function __get($key)
{
    $fw =& Controller::get_instance();
    return $fw->$key;
}
4

1 回答 1

4

那是因为构造函数View调用构造函数,Controller反之亦然。您需要重构代码以删除该循环引用。

我个人认为没有理由视图需要创建控制器,甚至不需要了解控制器。控制流应该是单向的:从控制器到视图。

如果您需要将功能从控制器注入到视图中,您可以为其分配回调。像这样:

class Controller {

    function __construct() {
        $this->view = new View();
        $this->view->setFooFunction(function() {
            // do some stuff
        });
        echo $this->view->render();
    }

}


class View {

    protected $foo_function;

    public function __construct() {
        // ... no controller required :)
    }

    public function setFooFunction(Closure $function) {
        $this->foo_function = $function;
    }


    public function render() {
        $this->foo_function->__invoke();
        ... 
    }

}
于 2013-07-24T12:03:10.560 回答