Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Class Inheritance - Out of Memory

Tags:

oop

php

class

I see everywhere that you cannot extend multiple classes...i.e.

Class Foo{
    //DO FOO STUFF HERE
}

Class Bar{
    //DO BAR STUFF HERE
}

Class FooBar extends Foo,Bar{
    //DO FOOBAR STUFF HERE
}

That makes sense, but what about:

Class Foo{
    //DO FOO STUFF HERE
}

Class Bar extends Foo{
    //DO BAR STUFF HERE
}

Class FooBar extends Foo{
    //DO FOOBAR STUFF HERE
}

For example, can you have multiple child classes extend the same parent? When I try this, I get an out of memory error, and I tried increasing the memory limit to 512MB but I still get out of memory errors...

What is a way to check memory usage, I mean these are very basic classes, i.e. one public variable, and the Bar class sets the value for the variable in the Foo class...It's a constant thing, but if I wait 15-20 minutes and try again, it is fine. My issue is, this server has 32GB of Ram, and I have even tried the stupid setting memory to 1024M as well.

[Edit Code:]

Class CORE{
    public $load;
    public $data;
    public $models;
    public function __construct(){
        $this->load=New Loader;
        $this->data=New Database;
        $this->data->db=New PDO(DSN);
    }
    public function __dev_email($subject, $message){
        $to="[email protected]";
        $headers="From: \"Error Checking\" <[email protected]>";
        mail($to, $subject, $message, $headers);
    }
}
class Loader extends CORE{
    public function model($name){
        if (file_exists("/var/www/models/".$name.".php")){
            require_once("/var/www/models/".$name.".php");
            $models[$name]=New $name;
        }
    }
}
class Error extends CORE{
    $this->__dev_email("Bad Database Connection", "Invalid Connection Attempt was made. Please check the configuration.");
}

Keep in mind this is not all of the code, there is an Autoloader, and several different files, but this is a basic version, and I just checked, I am still getting out of memory errors from this.

like image 462
Justin E Avatar asked Feb 17 '26 00:02

Justin E


1 Answers

The problem is that when you are initializing CORE, it creates a new instance of Loader which extends CORE. This causes the CORE constructor to be called again, which in turn creates another Loader meaning it continues forever leading to the out of memory errors.

One way which might work is passing the CORE to the Loader class on initialization as shown below.

Class CORE{
    public $load;
    public $data;
    public $models;
    public function __construct(){
        $this->load=New Loader($this);
        $this->data=New Database;
        $this->data->db=New PDO(DSN);
    }
    public function __dev_email($subject, $message){
        $to="[email protected]";
        $headers="From: \"Error Checking\" <[email protected]>";
        mail($to, $subject, $message, $headers);
    }
}
class Loader{
    private $core;

    public function __construct($core){
        $this->core = $core;
    }

    public function model($name){
        if (file_exists("/var/www/models/".$name.".php")){
            require_once("/var/www/models/".$name.".php");
            $this->core->models[$name]=New $name;
        }
    }
}
class Error extends CORE{
    $this->__dev_email("Bad Database Connection", "Invalid Connection Attempt was made. Please check the configuration.");
}
like image 86
Jayden Meyer Avatar answered Feb 19 '26 12:02

Jayden Meyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!