RE: [PHP] OO Design and Implementation in PHP

From: Date: Tue, 15 Aug 2000 03:14:26 +0000
Subject: RE: [PHP] OO Design and Implementation in PHP
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-11727@lists.php.net to get a copy of this message
We use a gate on each include file. Every include file will conditionally include all required files, testing for the constant associated w/that header file. This method is shown by Lakos in "Large Scale C++ Software Design" to reduce overhead for includes to a minimum - only those files needed are opened and only once. Assumptions: one class per file, all directories are at same level (all use "../inc/xxx.ph" - more flexibility could be accomplished by writing your own include funcion or having the include path work correctly). We're using an older version of PHP, so the include_once() function might do some of this overhead for you. Hope this helps more than it confuses. Dan Sevush CTO Dan's Chocolates (soon to be OneSage, a "solutions" company:) CBase.ph: <? if (!defined( "CBASE_PH" )) { define( "CBASE_PH", 1 ); class CBase { // define class here } // end of class } // end of if defined(! CBASE_PH ) ?> CAppFeature.ph: <? if (!defined( "CAPPFEATURE_PH" )) { define( "CAPPFEATURE_PH", 1 ); // make sure the base class is known if (!defined( "CBASE_PH" )) include( "../inc/CBase.ph" ); class CAppFeature extends CBase { // define class here } // end of class } // end of if defined(! CAPPFEATURE_PH ) ?> > -----Original Message----- > From: Jason Crane [mailto:jason@mediasat.com.au] > Sent: Monday, August 14, 2000 10:41 PM > To: php-general@lists.php.net > Subject: [PHP] OO Design and Implementation in PHP > > > 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? > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: > php-list-admin@lists.php.net >

« previous php.general (#11727) next »