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!