I understand the basics of inheritance but this one is confusing me. How would you go about saying:
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
The basic principles of OOP involves Abstraction, Encapsulation, Inheritance, and Polymorphism. There are also objects and classes. Together, they stand as the working principle of any object-oriented programming language. In this post, we have covered all these basic principles of OOP in a jargon-less format.
In object-oriented programming (OOP), objects are the things you think about first in designing a program and they are also the units of code that are eventually derived from the process.
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.
As I mentioned in one of my comments, if you had to model it via OOP I would say,
class Song {
string[] artists;
string title;
int genre;
}
class Album {
Song[] tracks;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With