OO Design and Implementation in PHP

From: Date: Tue, 15 Aug 2000 02:41:04 +0000
Subject: OO Design and Implementation in PHP
Groups: php.general 
Request: Send a blank email to php-general+get-11723@lists.php.net to get a copy of this message
I'm not sure if this issue has been addressed, but I shall persevere none-the-less. =) I'm currently undergoing a redesign of a site that I wrote in PHP haphazardly. The plan is to make the entire site OO driven, in that the index.php file only contains the instantiation of of the super class which runs the whole show. My problem is thus: I have a class called Database, contained in the file classes/core/Database.php. Another class, Article (contained in classes/core/Article.php) uses the Database class to load in an article. At the top of the Article.php file, I have a "require" clause to require the Database class, as PHP spits the dummy if I try to declare a class within a class. So far, this all sounds simple and indeed, it is not rocket science. My problem arises when I develop the site a bit more, and have two classes being included which both want to instantiate the Database class. There is a high probability that index.php will eventually contain half-a-dozen or so classes that all want to do database calls and require instantiations of the Database class. What I want to avoid, is an index.php file with a dozen or two "require" lines, that include all classes that will typically have multiple instantiations - what I'm trying to find out is if there is a way to create a "singleton", or some work-around that would keep the declaration of the Database class to each class that requires it, so at the end of the day, if I do another project I can simple copy .php files, and not have to worry about which files I need to include at a higher level. I've looked around a little bit on the 'net for PHP projects that are OO and on a largish scale, and as yet have not had much luck (perhaps my searching skills leave a lot to be desired). The only work-around I've come across so far is a post to this list about doing an IFDEF style solution on a per class basis. Another suggestion I've had is to make each class inherit the database class, and I guess this would work (not yet tested of course), but wouldn't I run into the same "must include parent class inside file" issue? Also, this is not really the true concept of "inheritance" (I'm also trying to make this project as true to OO as possible, otherwise I'll stick to spaghetti scripts ;) [deep breath] What I'm looking for, is a more elegant solution to implementing multiple instances of base classes, without abstracting the declaration to the highest .php file, OR a site or someone who has written a largish PHP OO system that I can have a look at. Any takers?

« previous php.general (#11723) next »