dongxing2263 2013-03-27 21:14
浏览 27
已采纳

在PHP中编写OOP时出现意外错误?

For some reason I get this error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in C:\xampp\htdocs\school\blom\inlog dinkie\engine\class.php on line 13

On this piece of code:

<?php

class gebruiker extends start{

    private $_login_form;
    public $log_in;
    public $logged_in;

    function __construct(){

        $this->logged_in = false;

        $this->_login_form = new login_form();

        parent::html($this->_login_form);

    }

    function log_in(){

        $html = $this->_login_form;

        if($this->log_in){

            $go = true;
            if(!$_REQUEST['naam']){
                $this->_login_form->error_naam = 'vul je naam in!';
                $go = false;
            }
            else{
                $this->_login_form->naam = $_REQUEST['naam'];
            }

            if(!$_REQUEST['pass']){
                $this->_login_form->error_pass = 'vul je pass in!';
                $go = false;
            }
            else{
                $this->_login_form->pass = $_REQUEST['pass'];
            }
            //go log in

            $html = $dom->saveHTML();
        }

        parent::html($this->_login_form->form());
    }

}

class login_form extends gebruiker{

    protected $html;
    protected $error_naam = '&nbsp;&nbsp;&nbsp;';
    protected $error_pass = '&nbsp;&nbsp;&nbsp;';
    protected $naam = ''; 
    protected $pass = '';

    function form(){
        $this->html = ' <center>
                            <div style="border:1px dotted rgb(169, 169, 169); width:572px; height:196px; background-color:rgba(40, 152, 250, 0.670588);margin-top:200px;">
                                <h4 id="title">Inloggen</h4>
                                <br>
                                <form action="./?login" method="post">
                                <table>
                                    <tr>
                                        <td id="error_naam">'.$error_naam.'</td>
                                        <td id="error_pass">'.$error_pass.'</td>
                                    </tr>
                                    <tr>
                                        <td><input id="naam" type="text" placeholder="naam" name="naam" value="'.$naam.'" /></td>
                                        <td><input id="pass" type="password" placeholder="wachtwoord" name="pass" value="'.$pass.'" /></td>
                                    </tr>
                                    <tr>
                                        <td><input type="submit" value="Inloggen" /></td>
                                        <td></td>
                                    </tr>
                                </table>
                            </div>  
                        </center>';
        return $this->html;
    }
}

?>

This is an back-end of this:

<?php

include_once('engine/database.php');
include_once('engine/class.php');

$start = new start();

class start{

    private $_html;

    function __construct(){

        session_start();
        if(isset($_SESSION['gebruiker'])){

            if(isset($_REQUEST['login'])){
                $_SESSION['gebruiker']->log_in = true;
                $_SESSION['gebruiker']->log_in();
            }
            elseif(isset($_REQUEST['register'])){
                //register
            }
            elseif(!$_SESSION['gebruiker']->logged_in){
                $_SESSION['gebruiker']->log_in = false;
                $_SESSION['gebruiker']->log_in();
            }
            else{
                switch($_REQUEST['actie']){
                    case 'iets':
                        //dostuf
                        break;
                    default:

                        echo 'deafauasdfasdr';

                        break;
                }                   
            }
        }
        else{
            $_SESSION['gebruiker'] = new gebruiker();
            //$new = new gebruiker();
        }
    }

    protected function html($html = 'emty'){
        $this->_html = $html;
    }

    function __destruct(){
        echo $this->_html;
    }

}

?>

Now I think that I get this error because I store the gebruiker() class inside a $_SESSION. I do not declare a lot of code so I don't understand why I reach a memory limit.

Or is it because I can't store a functional class with so many extends inside a $_SESSION?

  • 写回答

2条回答 默认 最新

  • duanchao9559 2013-03-27 21:27
    关注

    A memory allocation failure such as yours is usually due to an infinite recursion, in your case on line 13 (as specified by the error). The issue is due to constructor inheritance.

    When you extend a class, you also inherit the extended class's constructor if the said constructor is public (though defining a new one in your extending class overrides it). In your case, the flow goes as follows:

    • You create a start object
    • Your start object creates a gebruiker
    • Your gebruiker objects creates a login_form object (Which extends gebruiker and has no reason to do so), does not have an explicit constructor, and therefore runs gebruiker::__construct() on creation
    • This object creates a login_form of its own.

    This is where the infinite loop is happening.

    Two ways to fix it:

    Remove the inheritance: You lose nothing by doing so, as your login_form just outputs static HTML and does not actually require a single function of gebruiker.

    Add an explicit constructor definition to login_form: This will prevent the parent constructor from running.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?