2

I am currently trying to build myself a nice few classes to build my pages a bit faster.

I currently have this setup,

// page class
class Page extends DBC {
    // creates a page
    public function create($data = false) {
       echo 'SOME BASIC HTML TO SETUP PAGE';
       // pass the database connection var to content function
       content($this -> $dbc, $data);
       echo 'SOME BASIC HTML TO SETUP PAGE';
    }
}
// dbc class
class DBC {
    public $dbc;
    public function __construct() {
        // sets the database connection
    }
}

And for each new page I create I just do this after requiring my classes into the script.

// content function
function content($dbc) {
    // some content
    // am able to use dbc
}
// new page object
$page = new Page();
// am able to use dbc var fine here too
$page -> dbc -> // execute a query
// create page
$page -> create();

But the problem I face now is how best to create functions inside my DBC class to make my query building faster. I tried adding the following function to the class DBC,

public static function query($q, $data, $options = array()) {
    // execute prepared query
    $q = $this -> dbc -> prepare($q);
    return $q -> execute($data);
}

But I cannot use this inside my content function as only the database connection is passed into it and the query function inside the DBC class uses the connection in object context.

My question after explaining my problem is as follows;

Would it be best to make the functions that execute querys for me inside my DBC class static and just pass in the existing connection?

OR

Would it be better to pass the whole DBC object to my content function inside the Page class like below,

content(new DBC(), $data);

That way allowing me to use the functions in object context. The Page class still needs to be extended by the DBC class as I need to use the dbc var outside of the content function too. But wouldn't this then make 2 instances of the DBC class one when it is extended by Page and one when passed into the content function?

Thanks for taking the time to read this question and for any answers given!

Griff
  • 1,647
  • 2
  • 17
  • 27
  • "after requiring my classes into the script." -> [autoloading??](http://php.net/manual/en/function.spl-autoload.php) – PeeHaa Jun 14 '12 at 20:06
  • no I just do `require('classes.php');` as they are saved in a different file. – Griff Jun 14 '12 at 20:14
  • setting up an autoloader function effectively requires the files when you need them - or tries to, at least. That way, you're not requiring files you don't need. When you try to instantiate an undefined class, the autoloader will look for the files it needs to load, if the magic function is working properly, that is ;) furthermore: `require_once('classes.php');` is what you want: if the class is defined, no need to require the file a second time – Elias Van Ootegem Jun 14 '12 at 20:31

2 Answers2

1

You should look in to the MVC (Model-View-Controller) pattern. You don't want your html/Page code and your database code mixed together. If you're going to take the time to create a database access layer, you should go ahead and separate the display and data code.

If a full MVC separation is too much work for right now, I suggest you use the Repository Pattern for database access. It will give you the database methods you need in a nice concise system that you will be able to access from your Page classes.

The accepted answer to this question has good information for you: Setting up a repository pattern in MVC

Community
  • 1
  • 1
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
1

Going by what i understand, i think an example would be best so you can take a look and make improvements as per your requirements.
Check this class i wrote on github
And check out this gist on usage
So all I'm saying is, look at it and come up with a solutions that suits your needs or you could just use it... it's free of course

Emmanuel Okeke
  • 1,452
  • 15
  • 18