クラスSongを拡張したいクラスPlaylistTrackがあります。
Song を作成するときは、アーティスト、タイトルなどのデータの配列を渡します。
PlaylistTrack を作成するときは、すべての曲のメソッドとそれが導入する新しいメソッドを含める必要があります。
しかし、コンストラクターで特定の曲を渡したいとも考えています。
だから私はこれをやっています:
class Song {
function __construct( $song_data ) {
$this->_data = $song_data;
// etc
}
}
そして、これ。私を撃たないで!
class PlaylistTrack extends Song {
function __construct( $song ) {
// call the song's constructor again in the context of this class
// to give access to its methods.
parent::__construct( $song->_data );
// other PlaylistTrack-specific material to go here
}
}
控えめに言っても、これは独特な感じです。大丈夫ですか?代替手段はありますか?