Oliver Marach
Oliver Marach

Reputation: 31

Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes). What to do?

I keep getting fatal errors for the following code. What should I do to get rid of this error? I am trying to make an MVC Framework based site, the problem is with my Models. every thing else works fine.

<?php
class Model {
    private $db;
    private $session;

    public function __construct() {
        $this->db = new Database_Model;
        $this->session = new Session_Model;
    }
}

/**
 * Database Class
 */
class Database_Model extends Model {
    public function getUserInfo() {
        return array(
            'Thomas', 'Jane'
        );
    }
}

/**
 * Session Class
 */
class Session_Model extends Model {
    public function getUserId() {
        return $_SESSION['uid'];
    }
}

$b = new Database_Model;
$b->getUserInfo();
?>

Upvotes: 1

Views: 8875

Answers (1)

bfavaretto
bfavaretto

Reputation: 71908

You have created an infinite recursion scenario. Look:

  • Database_Model and Session_Model extend Model.
  • When you instance Database_Model, the constructor inherited from Model will instance two additional objects on the constructor, $this->db and $this->session.
  • Those new objects also inerit from Model, so they will also instance their own Database_Model and Session_Model.
  • And this goes on infinitely...

Upvotes: 11

Related Questions