When having two related objects, is it best to have them as one object or two separate?
For example, if you have an Artist object and an array of Album objects that relate to a single Artist.
<?php
// two separate objects?
$artist = $ArtistDataMapper->find(1);
$album = $AlbumDataMapper->allByArtist($artist->id);
// or one
$artist = $AristDataMapper->find(1); //datamapper will also include query for albums
It depends on what your purpose is. However, if you think about it as physical entities you might end up thinking of "references" to different entities. By this I mean that, you might have: * Artist object (id, name, birth date, etc.) * Album object (id, artistId, name, etc.)
Eventually, you might want to have even Discography objects between the two.
It's up to you and your requirements, anyway.
Classes and objects are abstractions. When defining them the program designer tries to separate separate parts of the code into logical entities that make sense in that respective context. The key concepts when doing OOP (Object Oriented design are abstraction
, encapsulation
and inheritance
, which I recommend you to study. Other side topics to study are SOLID, DRY, KISS, GRASP principles. Maybe some of them are complex, but some are quite simple. For example S
from SOLID stands for single responsibility. This principle suggests that one class should have only one purpose and define only one type of objects. In your case, the artists are a different logical entity than an album. An artist produces albums, which denotes that there is a relationship between these to, but they are not identical. An artist can do other things besides albums, like concerts or interviews, and an album can be produced also by groups of artists or by other entities, and in general have different attributes than an artist.