0

When I do the following:

class AA {
    public $a = '';

    function __construct() {
        $this->a = new BB();
    }
}

class BB {
    public $b = '';

    function __construct() {
        $this->b = new AA();
    }
}

I get Fatal error: Allowed memory size of X bytes exhausted.

Is it even possible to achieve what I am trying to do above?

What am I trying to accomplish:

Let's say I have objects:

Universe:
  Galaxy
  Galaxy
  Galaxy

Galaxy:
  Blackhole
  Star
  Star
  Star
  Star

Blackhole:
  Whitehole

Whitehole:
  Universe

Then, Universe in white hole is same as big universe and it would continue as above, recursively.

3
  • 3
    you defined 2 classes that create new instances of each others... of course you will get memory error... Commented Mar 25, 2018 at 17:33
  • 1
    you are doing wrong abstraction. it's more philosophy rather than programming. if creation of one thing from other thing is not essence/nature of it, why you describe them like that? better, create createBlackhole() method in your galaxy, createWhitehole() in your backhole class etc. the call the when needed. because galaxies does not exist for creating blackholes, or blackholes does not only exist to create whiteholes. so do not make such abstraction. if one day you'll make universe to be your compiler, be sure, it would also spit that error. Commented Mar 25, 2018 at 17:43
  • @marmeladze Yes, I get your idea and just tried it, it works. But, I'd like it to work recursively forever. Take a look at [this link] (orteil.dashnet.org/nested) and keep expanding always the first element. I want to do something similar to that. Commented Mar 25, 2018 at 17:56

1 Answer 1

3

In your code you create A, which creates B, which creates another A, which creates another B, and so on. So yes in the end you will run out of memory.

I guess what you trying to do is

<?php

abstract class Element {
    private $elements;
    abstract protected function createElements();
    public function getElements() {
        if(null === $this->elements) {
            $this->elements = $this->createElements();
        }
        return $this->elements;
    }
}

class Whitehole extends Element{
    protected function createElements() {
        return [new Universe()];
    }
}
class Blackhole extends Element{
    protected function createElements() {
        return [new Whitehole()];
    }
}
class Galaxy extends Element{
    protected function createElements() {
        return [new Blackhole(), new Star(), new Star(), new Star(), new Star()];
    }
}
class Universe extends Element{
    protected function createElements() {
        return [new Galaxy(), new Galaxy(), new Galaxy()];
    }
}
class Star extends Element{
    protected function createElements() {
        return [];
    }
}

$universe = new Universe();
$universe->getElements()[0]->getElements()[0];

We create elements as they are requested, which might provide good enough illusion of infinity

Sign up to request clarification or add additional context in comments.

5 Comments

A whitehole does not contain the original universe, but a new universe object.
@DonJoe well, you won't be able to create infinitely deep chain since you are using machine with finite memory.
@DonJoe I checked link you provided, they use model similar to my code, they just create new elements when you click
Then I guess the answer to my question is to create the new element dynamically. Add that to your answer and then I accept it. By the way, on [this link] (ibb.co/fEKSs7) you can see I got a *RECURSION* text from your solution. Is that Php compiler trying to protect stack overflow or something similar? I've never seen that kind of message before and I can't even find anything on google...
@DonJoe I changed my snipped to generate elements on demand. *RECURSION* means that you reached element that was already printed somewhere. Since you can't print tree of cycle, interpreter just breaks it

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.