I have 2 objects that are related. Album->Song. One album has many songs. Within the album class i have a method called GetSongs(). This does what you would expect.
I have a base class that contains generic functionality i use across all objects. Both album and song extend this object. The important method here is.
public function Get(){
return new static();
}
Both album and song have a method called ByID($postid)
. This method will take the ID, fetch the info from the DB and populate the blank object. This allows me to create an album like this. This code works fine.
$album = Album::Get()->ByID(1);
public function ByID($postid) {
$post = "sql to get info from db";
$this->Title = $post['title'];
.....
return $this;
}
The problem arises when I call a method called GetSongs()
. For some reason this method is returning Album objects.
public function GetSongs()
{
$songresults = "SQL and other code to get the songs from the db";
$songs = array();
foreach($songresults as $song) {
$songs[] = Song::Get()->ByID($songresults["ID"]);
}
return $songs;
}
This seems quite simple, but i dont understand what is going on. Song::Get()->ByID()
is actually calling the ByID method from the Album object and not the Song object, even though the code is referencing the song object.