So, I have an object (CD) which has as one of its member variables an array of
another object (Pš. My set function appears to work correctly...I can add new
objects to the array, and when I go back and dereference the array after
adding the object, I can see that it was actually added (via a simple
echo statement).
However, when I call my get function, the array suddenly is not an array
anymore. It has size 0, and array functions like reset() complain, saying
that what I am passing in is not an array.
This bug has stumped me for a while, and I haven't been able to find anything that
exactly addresses the problem on php.net. The manual does show a very very simple example of an array of a class, but what I have doesn't seem to work according to the what the example shows.
Here is the code that I am using:
Here is the set function:
function setArtistArray()
{
$this->m_artist = Array();
$artist_array = get_cd_artists($this->m_ID, 1); // get
main artist first
$len = mysql_num_rows($artist_array);
echo "In CD#setArtistArray()...the number of artists from
cds query == $len<P>";
$this->m_artist [] = new PB();
for($i = 0; $i < $len; $i++) {
$pbObj = mysql_fetch_object($artist_array);
$this->m_artist[$i]->init($pbObj->pb_id);
echo "I just added the following artist:
".$this->m_artist[$i]->getLastName()."<P>";
}
// now, get other artists if there are others
$artist_array2 = get_cd_artists($this->m_ID, 2);
$len2 = mysql_num_rows($artist_array2);
echo "In CD#setArtistArray()...the number of artists from
co_cd_band query == $len2<P>";
for($i = 0; $i < $len2; $i++) {
$pbObj = mysql_fetch_object($artist_array2);
$this->m_artist[$i + $len]->init($pbObj->pb_id);
echo "I just added the following artist:
".$this->m_artist[$i + $len]->getLastName()."<P>";
}
}
Here is the code in the get function:
function getArtistArray()
{
echo "Inside CD#getArtistArray(), with index 0 ==
".$this->m_artist[0]."<P>";
return $this->m_artist;
}
And here is the code that I am executing in the test script:
$cd = new CD(202); // initialize CD object from db
// other stuff
echo "This is the artist: ";
$artist_array = $cd->getArtistArray();
echo " (This is the number of artists: ".count($artist_array)." )
";
$artist = $artist_array[0];
echo $artist->getLastName()."<P>";
And finally, here is the output from all the echos:
This is the query: SELECT pb_id FROM cds WHERE cd_id='202'
In CD#setArtistArray()...the number of artists from cds query == 1
I just added the following artist: 77s
This is the query: SELECT pb_id FROM co_band_cd WHERE cd_id='202'
In CD#setArtistArray()...the number of artists from co_cd_band query == 0
This is the artist: Inside CD#getArtistArray(), with index 0 ==
(This is the number of artists: 0 )
Fatal error: Call to a member function on a non-object in
c:\public_html/drindus/test/test_cd.php on line 14
Any ideas? Why would the variable m_artist suddenly not contain anything? The set function does seem to work...it's almost like the array is going out of scope, which it shouldn't be.
Any help is greatly appreciated.
Thanks,
David