3

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.

12
  • 1
    Sure, you can extend a class that extends. What problems you are encountering is something in your //DO STUFF HERE implementations. Like, for example, if you were to instantiate an instance of an object in its own constructor leading to an infinite chain of instantiations... Commented Dec 3, 2013 at 18:26
  • yes, it's valid. you're basically just branching the family tree. Such inheritance isn't a simple sizeof(Foobar) + sizeof(Foo). The whole point of inheritance is that code is reused. Technically/vaguely, sizeof(Foobar) will be sizeof(Foo) + sizeof(any changes made in Foobar that aren't in Foo) Commented Dec 3, 2013 at 18:27
  • 1
    Let me post some code...one sec Commented Dec 3, 2013 at 18:28
  • 1
    Constructors are inherited too .. when you are initializing Loader instance, the CORE constructor is called .. which tries to initialize new Loader. Basically: you have endless loop. Also, i really hope that this is not some "mvc framework". We have had 4 such posts in past hour already. Commented Dec 3, 2013 at 18:41
  • 1
    @JustinE You shouldn't use inheritance here. Instead consider composition, perhaps by dependency injection. Commented Dec 4, 2013 at 3:21

1 Answer 1

1

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.");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.