edib
edib

Reputation: 872

implementation of relations between classes in oop php

I am new in PHP with classes. I am coding an network ip-mac-user logging system integrated with dhcp. I have users, subnets, units. I created classes for each and created parameters and functions to fill the parameters and some mysql codes about what they do. but there are relationships among these classes. where can I put these relations' codes, functions, for example there are m-n relations between subnets and units, where should I put the relationship codes?

Upvotes: 2

Views: 1216

Answers (2)

Petah
Petah

Reputation: 46050

In the class that relates to the other class. Just make sure you don't have circular references or you will run into memory trouble.

class Adult {
    private $children = false;
    public function get_children() {
        // This is where you get the related instances
        if ($this->children === false) {
            $this->children = db_fetch('children', $this->get_id()); 
        }
        return $this->children;
    }
}

Upvotes: 2

Related Questions