I'm building a music playlists plugin in PHP, and in an attempt to make my (big) code clear, I splitted each "pack" of functions inside classes, depending of what those functions are used for (eg. a class for the "cache" functions).
My main "playlist" object is passed inside each of those subclasses, because I NEED to access it all the time.
I did try to avoid passing the whole object, but I'm always stuck somewhere with that. So having my main object passed in subclasses is very convenient for me.
Here's my problem : the code works well, the page prints in the browser, but... the browser loading wheel keep spinning, as if the page had not finished loading.
I guess the thing is that there is a problem self-referencing the same object everywhere (and nesting it), but I don't really understand why and foremost, how I could avoid that. I got some hope when I saw the &$var thing (Passing by Reference), which seems to make a "link" and not a copy of a variable, but it hasn't resolved my problem, the wheel keeps spinning.
I apologize if my question is not totally clear. I have myself problems to understand the whole thing, and as english is not my first language, it's even more difficult to explain to you.
My code is quite big so i'll try to make an illustration of my problem here.
class Playlist{
var $cache;
function __construct(){
$this->cache = new PlaylistCache($this); //I guess this may produce problems
$this->admin = new PlaylistAdmin($this); //I guess this may produce problems
}
}
class PlaylistCache{
var $playlist;
function __construct($playlist){
$this->playlist = $playlist;
}
}
class PlaylistAdmin{
var $playlist;
function __construct($playlist){
$this->playlist = $playlist;
}
}
$playlist = new Playlist();
Do you think those references are causing my problem ? Thanks !
It's hard to tell without looking at how you're using the code you posted, but it looks like you have your class inheritance backwards. Instead of your Playlist class having the other two classes in it, you should have the other two inherit the methods from your playlist class.
This way, your Playlist class will be the Base class that has all the common methods the other two classes may need.
class PlaylistCache extends Playlist
{
// this class has all public methods from Playlist
}
class PlaylistAdmin extends Playlist
{
// this class has all public methods from Playlist
}
The link above should have more on this. Hope it helps.