How can I include objects in each other using PHP OOP?

I have 3 classes all classes make use of one another. I am trying to call them on __construct but it's creating an infinite calls to one another. How can I solve this?

Recommended Answers

All 8 Replies

I'm not understanding your question. You can definitely create objects within the methods of other classes. As pritaeas says, please post some code so we can understand better.

In every OOP language that I know you can create infinite recursive loops in a construction method . Lets play a bit with it in PHP:

class A
{
    private $obj; 

    public function __construct()
    {
        $this->obj = new B();
    }
}

class B
{
    private $obj;

    public function __construct()
    {
        $this->obj = new C();
    }
}

class C
{
    private $obj;

    public function __construct()
    {
        $this->obj = new A();
    }
}

$obj = new C();

But why on earth would you do that ?

commented: Thank you for this example +34

Hey, I know that this discussion is old but I need to know now.
I am new to OOP in PHP. Is it good to call a class within a class? Why or why not?
How should I create an object? I want objects to use other methods from other objects.
Your example is good ...

class A
{
    private $obj; 

    public function __construct()
    {
        $this->obj = new B();
    }
}

class B
{
    private $obj;

    public function __construct()
    {
        $this->obj = new C();
    }
}

class C
{
    private $obj;

    public function __construct()
    {
        $this->obj = new A();
    }
}

$obj = new C();

but I am trying to do the following ...

class A
{
    private $A;
    private $B;
    private $C;

    public function __construct()
    {
        $this->A = new A();
        $this->B = new B();
        $this->C = new C();
    }
}

class B
{
    private $A;
    private $B;
    private $C;

    public function __construct()
    {
        $this->A = new A();
        $this->B = new B();
        $this->C = new C();
    }
}

class C
{
    private $A;
    private $B;
    private $C;

    public function __construct()
    {
        $this->A = new A();
        $this->B = new B();
        $this->C = new C();
    }
}

$A = new A();
$B = new B();
$C = new C();

That causes an infinite loop as described by jkon.

I'd go for a container class:

class Container
{
    private $A;
    private $B;
    private $C;

    public function __construct($A, $B, $C)
    {
        $this->A = $A;
        $this->B = $B;
        $this->C = $C;
    }
}

$A = new A();
$B = new B();
$C = new C();
$Container = new Container($A, $B, $C);

https://www.daniweb.com/programming/web-development/tutorials/437592/introduction-to-php-s-object-orientation

I feel like you're going to end up in an infinite loop if you create a new A from the constructor function of class A. Same with class B having new B() in its contruxtor, and and C having new C() in its constructor.

Oh, sorry. I didn't see the post by pritaeas.

To avoid infinite loops when including objects of multiple classes in PHP OOP:

  1. Use dependency injection to pass objects as dependencies.
  2. Review class design for circular dependencies.
  3. Refactor classes to break cyclic dependencies if necessary.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.