Combining Objects Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
linchay
Forum Newbie
Posts: 7
Joined: Thu Mar 06, 2008 1:34 pm

Combining Objects Question

Post by linchay »

Hi all,

Have a question and wandering how some of you, or what would be the best practice for this implementation.

I have three objects

Company
Categories
Product


Each of these objects are standalone objects, but at times, the three could be combined to create a complete product.

One way is:

categories extends company
product extends categories

THis will work, but this extended object just seems like over and it is not something I need. I wish something in php would allow me to creare a parent like this

class productComplete extends product, categories, company {}

But since php does not allow this, how would you design something like this? What is best implementation?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Combining Objects Question

Post by Christopher »

You would probably be better off using composition. Inheritance has its place, but generally try to composite first.

Code: Select all

class categories {
    protected $company;
 
    public function __construct($company) {
        $this->company = $company;
    }
}
 
class product {
    protected $category;
 
    public function __construct($category) {
        $this->category = $category;
    }
}
If you have one to may relationships they you can pass in an array or have an add() method to append to the list.
(#10850)
Post Reply