【问题标题】:Maximum Nesting Level of 100 Reached, Aborting! - PHP已达到最大嵌套级别 100,正在中止! - PHP
【发布时间】:2013-07-23 21:25:17
【问题描述】:

致命错误:达到“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;
}

【问题讨论】:

  • 我假设你的循环之一坏了......
  • 弄清楚为什么 View 构造函数会尝试构造一个 Controller。
  • 您的ViewController 之间有一个循环引用。
  • 好吧,我需要它能够将 $this->view、$this->model 等连接到视图和模型中。视图+模型控制器(以及该文件中的所有其他控制器)不扩展控制器,但我希望能够在视图/模型中使用它们,所以我尝试扩展控制器,我看到它不想玩得很好.
  • 正如其他人所建议的,这是一个设计问题。您是否考虑过其他方法?显然你正在撞墙,为什么不简单地从方程中删除墙?

标签: php class


【解决方案1】:

这是因为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();
        ... 
    }

}

【讨论】:

  • 其背后的要点是能够从内部视图和模型中使用 $this->view、$this->model 等。它在控制器中运行良好,但在视图/模型中不行。
  • 如果您的模型与视图通信,则说明您有严重的设计问题
  • 它没有,但我希望能够调用控制器定义的 $this->{method},例如库、输入、会话等。
  • 无论如何。您需要从View::__construct() 中删除new Controller()
  • 我确实删除了它,但我如何调用 $this-> 输入例如?
猜你喜欢
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多