This is my general php page:
<?php
require_once('includes.php');
require_once('cms.class.php');
.....
rest of the page
?>
in includes.php an pro object called $db is initiated which I want to use in the class specified in the cms.class.php
includes.php:
$db = new PDO('mysql:host=localhost;dbname=xxxxx','xxxxx','xxxxxx');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
How can I use this database object in my classes without having multiple places where my credentials are stored?
You
needwant a dependency manager or a bootstrapper or whatever you want to call it.This approach scales very well and can handle any dependecy. It also aims to put all the settings in one place so you don’t have configuration settings littered everywhere. The
$dmis the only one who knows the settings, if you need to construct something based on the settings, put it in the$dm.