views:

45

answers:

2

I'm trying to organize classes in a project in a fashion similar to:

$TheRoberts = new family;

class family {
    public function __construct() {
        $bobby = new father;
        $cindy = new daughter;
    }
}

class father extends family {
    function PayBills() {}
    function GoToWork() {}
}

class daughter extends family {
    function AskForMoney() {}
    function GoToSchool() {}
}

Literature on the subject is pretty abstract, but, if I understand correctly, this is how it's done. Why, then, is PHP throwing errors about memory exhaustion and exceeding execution times? In short: why is the constructor looping?

+4  A: 

Calling new family is then calling new daughter, which is itself a child class of family, so therefore calling the family constructor which is calling new daughter...

Daughter and father should not extend family... You should create a family_member class which daughter and father extend (an is-a) relationship. Then the family could have family_members (has-a relationship).

Mike Sherov
Jordan, thanks for editing. My iPhone seems to like to replace constructor with constrictor.
Mike Sherov
+1  A: 

Your father and daughter classes don't define a constructor, so they will inherit the constructor from family. This will cause infinite number of father objects to be created.

In this particular example, neither father or daughter should extend the family class.

konforce