Reputation: 4432
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 = ' ';
protected $error_pass = ' ';
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
?
Upvotes: 0
Views: 124
Reputation: 19672
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:
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!
Upvotes: 1
Reputation: 15629
Thats because you get recursion in the constructor of gebruiker
. You create in the constructor of gebruiker a new login_form object, which inherits from gebruiker and because you inherit the __construct
method of gebruiker as well, you create more and more login_forms and finally a memory error
Upvotes: 3