I am currently learning OOP concepts. I have used CodeIgniter, I know it has OOP concepts but I don't understand how it works. I just use the methods in the documentation.
I am now on the inheritance part.
Here is my code:
<?php
class Artist {
public $name;
public $genre;
public $test = 'This is a test string';
public function __construct(string $name, string $genre) {
$this->name = $name;
$this->genre = $genre;
}
}
class Song extends Artist {
public $title;
public $album;
public function __construct(string $title, string $album) {
$this->title = $title;
$this->album = $album;
}
public function getSongArtist() {
return $this->name;
}
}
$artist = new Artist('Joji Miller', 'Lo-Fi');
$song = new Song('Demons', 'In Tounges');
echo $song->getSongArtist(); // returns nothing
From what I understand inheritance will let me access properties and methods from the parent class.
In my example I have instantiate the Artist. So now I have Joji Miller as artist name.
Now, if I instantiate the Song class, I thought that I can access the artist name since I am extending the Artist class. But it is just empty.
Would you help me understand why it is not getting the artist name?
Hope I explained myself clearly. Thankyou..
SonganArtist? Could you substitute aSongany place you'd use anArtist? … Then those two things have nothing in common and should not inherit each other.Song,ArtistandAlbum, and you pass them to each other in some order that makes sense. E.g.:$ar = new Artist('Foo'); $al = new Album('Bar', $ar); $al->addSong(new Song('Baz', $ar));