2

I am getting the following error when using the code below the error. can you tell me what i am doing wrong?

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in D:\dev\TESTCASE_classes_layout\main.class.php on line 18

Here is the code I use :

<?php

include('test_class1.php');
include('test_class2.php');
include('test_class3.php');

class Main_Class{

    protected $test_class1;   
    protected $test_class2;
    protected $test_class3;

    private $objects_array = array();


    public function Main_Class(){

        $this->test_class1 = new Test_Class1();
        $this->objects_array['test_class1'] = $this->test_class1; 
        $this->test_class2 = new Test_Class2();
        $this->objects_array['test_class2'] = $this->test_class2;
        $this->test_class3 = new Test_Class3();
        $this->objects_array['test_class3'] = $this->test_class3;

    }

    public function get_Objects(){

        return $this->objects_array;

    }

}

?>

Here is the code I use for all three test classes. It is exactly the same code only for class name number and the function name number.

<?php

class Test_Class1 extends Main_Class{

    function test1(){
        return 'hello';
    }

}

?>

It has to do with the extending part. Cause when I delete the extending part it works.

Here is my goal:

I am trying to instantiate the classes in this class and extend from it so all classes can call each other without generating another instance of the class.

maybe there is a better way of doing that so if you know that then let me know.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Controvi
  • 385
  • 1
  • 2
  • 13
  • 4
    What exactly is `the extending part`? This script runs fine on my machine. Please try boiling the problem down to a single demonstration file, post that on [codepad.org](http://codepad.org/) or [ideone.com](http://ideone.com/), and then give us a link to it. – phihag Jan 02 '12 at 13:49
  • sorry i edit the code. i made a little error. see the extending part for test_class1 – Controvi Jan 02 '12 at 14:53
  • Oh, looks like you have uncovered a bug in php. But your code is invalid nevertheless, you simply can't instantiate subclasses in a constructor. I'll write an answer once I've published the php bug .... – phihag Jan 02 '12 at 15:09
  • i am looking forward to the your answer – Controvi Jan 02 '12 at 15:20
  • Oops, disregard that statement. The segfault was caused by my local configuration. The current answers pretty much cover what I wanted to write, but I'll summarize it. – phihag Jan 02 '12 at 16:05

4 Answers4

1

Dude this memory exhausted error is occurring because all three classes are extending the main class and in the constructor of the mail class you are calling the all 3 classes..

Which means when you will try to create the instance of any of the child classes they will go to parent class which will in turn call them again..Hence you are in a infinite loop..and php memory is exhausted..

That's why it is giving error if you are trying to to extending..and working preety well without that..

now the next part is how to get attributes of other 2 classes..

First of all I don't see any reason you need such case, you can just define all the functions in a class and extend it in all and call them... If you still persist you have no other choice but creating instance seperately...:)

Rajat Singhal
  • 10,896
  • 5
  • 35
  • 54
1

You're getting infinite recursion. Try adding a constructor function in Test_Class1 such as

function Test_Class1 () {}

to prevent the call to the base constructor when instantiating Test_Class1

EDIT: As a general note, you should never instantiate the same class or a child class in a class constructor as this leads to your issue, infinite recursion. Luckily for you, PHP does not automatically call the parent's constructor when inheriting and specifying a child constructor. However, in most other OO languages, this pattern would break 100% of the time.

Damp
  • 3,300
  • 18
  • 18
  • i tried it because i knew that that parent constructor wont be called when extending. but i know now that i cant/not a good idee at least to instantiate a class in the constructor – Controvi Jan 02 '12 at 15:16
1

A constructor is called everytime you create an object of type Main_Class, or one of its subclasses.

Since you're creating new objects of a subtype during this creation, you run into an infinite loop that ends when memory is exhausted or the memory limit is reached:

new Main_Class()            calls ...
Constructor of Main_Class   calls ...
new Test_Class1()           calls ...
Constructor of Test_Class1  calls ...
Constructor of Main_Class   calls ...
new Test_Class1()           calls ...
...

Instead, you should create the objects in a static method. But even if you do, note that your goal violates the important principle of encapsulation and will lead to an extremely poor design. Instead, you probably want to have methods that operate on an arbitrary object of type Main_Class, and override the methods (i.e. have a method test in every one of these classes).

phihag
  • 245,801
  • 63
  • 407
  • 443
0

Check this thread Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php as it may help you out. It actually might not relate with your script but with your configuration.

Also check instantiation as it might be tricky to read the memory use given by the error. Maybe there's a loop of some kind. Are you using some framework with plugins or so?

Community
  • 1
  • 1
Alfabravo
  • 7,232
  • 6
  • 43
  • 77