Php 新的$variable和新的my_模型之间有什么区别?

Php 新的$variable和新的my_模型之间有什么区别?,php,class,variables,Php,Class,Variables,我一直在制作自己的PHP框架,我遇到了这个问题,我找不到答案,因此,为了使其成为一种模式,我以这种方式编码: class post_control extends LoopControl { var $model; function __construct() { parent::__construct(); $classname = get_class($this); $this->model = new $cla

我一直在制作自己的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中,
$classname
post_控件
,而不是
post_模型
,允许的内存大小为134217728字节。您正在递归调用构造函数。无限递归会消耗所有可用内存,从而产生致命错误。

是的,您正在耗尽所有系统内存。这是因为构造函数创建了一个无限递归:

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_控件
后,它将尝试为其
$model
属性创建另一个
post_控件
。这反过来将调用同一个构造函数,该构造函数将尝试为自己的
$model
创建一个新的
post\u控件
,以此类推,直到永远……

在您的示例中,基本上是无限次地创建
post\u控件的实例。这就是脚本消耗所有内存的原因

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
}

}

如果手动执行,则实例化
post\u模型
,但通过变量,则递归实例化
post\u控件
。只需调用构造函数,就可以进入兔子洞。jep,你最终会得到一个无限递归…换句话说,你在这里做两个完全不同的操作,其中一个是递归的,会消耗你所有的内存。您是想对
$classname
执行一些字符串操作以将其转换为模型名吗?Fu。哈哈,是的。。。我得把它炸开,然后连上“模型”谢谢老兄!这有点让人分心。。泰!赞成!谢谢这是一种破坏,它现在起作用了!谢谢ppl!新代码:>$aux=explode(“_”),get_类($this));$classname=$aux[0]。“\u model”;$this->model=new$classname();泰!但愿我能投赞成票!hehe@rcBytes:np!干杯泰!我的新代码正在运行!这有点让人分心,谢谢<代码>$aux=explode('''u',getclass($this);$classname=$aux[0]。''u model'