OOP: How would you set up an Artist, Album & Song relationship in OOP

class, inheritance, oop, php

Solution

An album has one or more artist objects. Inheritance would mean an album is an artist. What you need is (EDIT) aggregation:

class Album
{
  public $artists = array(); // Array of Artist instances
  public $songs   = array(); // Array of Song instances
}

Aggregation means every Artist and Song instance may belong to other Albums. With composition, an artist or song may belong to a single Album.

Problem

I understand the basics of inheritance but this one is confusing me. How would you go about saying: - An album object has one or more artist objects - An album object has one or more song objects My current code only will only allow one song per object: ``` class Song extends Album{} class Album extends Artist{} ``` I'm sure i'm overlooking something important. Any thoughts? I'm doing this in PHP

Original source