How do I solve this class declaration problem?

I’ve two classes which are as follows:

class first {
  
  // Somewhere in this class inside a function, class second's 
  // object is declared.
}

class second {
  // Another class
}

When I execute the script using a function inside class first it shows memory exhausted error.

I tried shifting class second at top and class first at bottom and it worked.

But I’m in situation where I cannot do that.

Is there any other way to do that and still declare class seconds object inside class first while class second is declared at bottom only?

NOTE: Both the classes are declared in the same file only.

How about posting the actual code you are having a problem with. Pretty hard to debug comments.

1 Like

It is not clear (to me) what “declaring” a class is supposed to mean. This code works just fine:

<?php
class first {
    public function createSecond() {
        return new second();
    }
}
class second {
}
$first = new first();
$second = $first->createSecond();

Based on your rather vague error message I would speculate that you have some class trying to create itself and getting into a loop.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.