Results 1 to 8 of 8

Thread: [RESOLVED] Classes

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Resolved [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?

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.


    Quote 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.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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 :-/

  5. #5
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    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?

  7. #7
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Classes

    Quote 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
  •  



Click Here to Expand Forum to Full Width