|
-
Jun 3rd, 2008, 04:21 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Classes
Ok i've just started to use PHP5, got a database class set up to handle all my database querys etc.
Do I need to declare a new instance of the database class on every page?
PHP Code:
require_once('database.php');
$a = new DB();
$a->connect();
Its just i have a class to handle the database and a class to handle my current logged in user. Which I will need to refresh on every page (SO reload and get all the info) Is this correct way of doing it?
-
Jun 3rd, 2008, 04:55 PM
#2
Hyperactive Member
Re: Classes
That's what I would do, but if it was on every page that database.php was included on, I would place it at the bottom, so it's always defined. Plus I would have __construct() automatically call connect(), or move the connection code into the constructor.
Aside from this I'm sure you'll perhaps get someone telling you to make a static class if you don't want seperate instances.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jun 4th, 2008, 01:25 AM
#3
Re: Classes
Database class should be either static or singleton pattern, depending on your opinion (and in my case, mood). Multiple database connections are rare; if you might need to handle these in the future but not at the moment, then singleton is a better choice than static. If you do need to handle them right now, then a regular class is needed.
 Originally Posted by Pino
Do I need to declare a new instance of the database class on every page?
You do not have to declare it in every script file if the files are designed to be included into a script which has the class declared in its scope.
If you have multiple entry points into your application (i.e. you aren't redirecting all requests to a controller script) then I suggest instantiating the database class either at the end of the database script file (as Rudi suggests) or in a global bootstrap script. Then include the appropriate script at the top of every entry point.
If you use one entry point, you can do all of your bootstrap stuff there. Simple.
-
Jun 4th, 2008, 05:27 AM
#4
Thread Starter
PowerPoster
Re: Classes
Pengate,
Forgive me, the current set up i have is an application.php file included on every page. At the moment this file looks like so...
PHP Code:
//Start Session session_start;
$class_directory = "classes/";
//Load the classes we will be using require_once($class_directory . 'company.php'); require_once($class_directory . 'database.php');
//Set up objects $db = new Database(); $company = new Company();
So are you saying I only need to set these objects up once?
Sorry for my limited knowledge :-/
-
Jun 4th, 2008, 05:31 AM
#5
Hyperactive Member
Re: Classes
Yes that's basically what I suggested, it's a singleton design - you only have one instance of a class that's accessible in the global scobe.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jun 4th, 2008, 05:32 AM
#6
Thread Starter
PowerPoster
Re: Classes
SO i wouldn't need to include that file on every page? And the variable $DB will be accessible from this point onwards on all pages?
-
Jun 4th, 2008, 05:35 AM
#7
Hyperactive Member
Re: Classes
Well you would include application.php on every page, which would thus include your classes and create them.
Once included, $db and $company will be accessible throughout the script.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jun 4th, 2008, 05:38 AM
#8
Thread Starter
PowerPoster
Re: Classes
 Originally Posted by RudiVisser
Well you would include application.php on every page, which would thus include your classes and create them.
Once included, $db and $company will be accessible throughout the script.
Right I see thats fine mind was wondering then.
Thanks for the help.
Pino
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|