0

私は独自の PHP フレームワークを作成してきましたが、この問題が発生しました。答えが見つからないため、この方法でコーディングしたパターンとして作成します。

class post_control extends LoopControl
{
    var $model;

    function __construct()
    {
        parent::__construct();
        $classname = get_class($this);
        $this->model = new $classname();
    }

}

私はこのエラーが発生します:

致命的なエラー: 23 行目の /opt/lampp/htdocs/LoopWork/admin/core/model/Form.class.php で 134217728 バイトの許容メモリ サイズが使い果たされました (77 バイトを割り当てようとしました)。

質問2:

私は本当に多くのメモリを使用していますか?どのように ?

事実 1:

new post_model(); をコーディングすると、このエラーは発生しません。

事実 1 を考慮すると、クラスの新しいスタンスを作成するこれらの方法には違いがあると思います。

もうありがとう!

4

3 に答える 3

1

あなたの例では、基本的にpost_control無限回のインスタンスを作成します。これが、スクリプトがすべてのメモリを消費する理由です。

class post_control extends LoopControl
{
    var $model;

    function __construct()
    {
        parent::__construct();
        $classname = get_class($this);
        $this->model = new $classname(); // this is the line where you instantiate a 
        //new post_control, and again post_control constructor is call, and a new instance
        //is created.. and so on for infinite times
}

}
于 2013-10-27T13:01:37.167 に答える
0

はい、システムのメモリをすべて使い果たしていますこれは、コンストラクターが無限再帰を作成するためです。

class post_control extends LoopControl
{
    var $model;

    function __construct()
    {
        parent::__construct();
        $classname = get_class($this); // evaluates to 'post_control'
        $this->model = new $classname(); // equiv to new post_control();
    }

}

を作成すると、そのプロパティ用にpost_control別の を作成しようとします。次に、これは同じコンストラクターを呼び出して、それ自体のために新しいものを作成しようとし、それを永遠に続けます...post_control$modelpost_control$model

于 2013-10-27T13:01:30.787 に答える
-1

$classname is post_control, not post_model in your example. You are calling the constructor recursively. Infinite recursion consumes all the memory available, thus produces fatal error.

于 2013-10-27T12:59:13.730 に答える