1

I would like to use parent/child classes in php, but not in the way of subclasses. As an example, let's say we have a class House and a house has Doors and Windows.

class House {
}

class Door {
}

class Window {    
}

And we have two types of doors, let's say the garage door and the front door.

class GarageDoor extends Door {
}

class FrontDoor extends Door {
}

How can I create the relationship between House and Door and between House and Window such that when I create a door, there must be at least a house and I should know the specific house. And when I delete a house, also its doors and windows should be deleted. How can I do that?

2
  • The Factory pattern will help with managing creation of objects in the correct way and using dependency injection. The Observer pattern can let you listen for objects to be deconstructed and perform an action. Commented Dec 21, 2016 at 17:01
  • The door is a component in a house, so you probably want a class property in House for Door objects (or arrays of Door objects if you have multiple doors). Similarly there can be a reference to the House in the Door, although this might lead to coupling, which I don't think matters in this case. Commented Dec 21, 2016 at 17:01

1 Answer 1

2

Not saying it's the best or even a good way to go about it, but it should give you something to play around with and trying yourself to experiment with different things :)

class House 
{
    /**
     * An array of all doors that have been installed in the house.
     */
    private $doors = [];

    /**
     * You can install a door in a house.
     */
    public function installDoor(Door $door)
    {
        $this->doors[] = $door;
    }
}

class Door
{
    /**
     * A reference to the house this door is installed in.
     */
    private $house = null;

    /**
     * A house is required before a door can be created.
     */
    public function __construct(House $house)
    {
        $house->installDoor($this);
        $this->house = $house;
    }
}

$house = new House();
$door = new Door($house);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. It helped a lot. I only changed the constructor of House, since I don't like houses without any doors. So in the constructor of House I added $this->doors[] = new FrontDoor($this).
In that case you could also make the House constructor require a FrontDoor: public function __construct(FrontDoor $frontDoor), that way you have control over which front door the house is using, because you can not create a new House without giving it a FrontDoor :)
Although that would make you run into problems with the example I wrote, as you can't create a Door without a House and can't create a House without a Door ;) So that's when we would think about what direction dependencies should go

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.