So I basically have two files. At the end I would have more, but I would like to create a class called DB that would use PDO database operations, and then I would extend this class to make all of my functions for working with the database. So DB class, would then extend to class dbADD, that would have all the add functions for different database tables.
This is called config.php:
<?php
DEFINE ('DBHOST', 'localhost');
DEFINE ('DBUSER', 'REMOVED');
DEFINE ('DBPSW', 'REMOVED');
DEFINE ('DBNAME', 'REMOVED');
class DB {
public $db;
private static $instance;
public function __constructor(){
$config ['db'] = array(
'host' => DBHOST,
'username' => DBUSER,
'password' => DBPSW,
'dbname' => DBNAME,
);
$this->db = new PDO('mysql:host =' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'],$config['db']['username'],$config['db']['password']) ;
}
public static function getInstance()
{
if (!isset(self::$instance))
{
$object = __CLASS__;
self::$instance = new $object;
}
return self::$instance;
}
public function GetArticles ($search){
$sql = "SELECT `FirstColumn`, `SrcColumn`, `article` FROM `test_table` WHERE `FirstColumn` = 23";
//$dbs = new DB();
$dbs = DB::getInstance();
$query = $dbs->db->prepare($sql);
//$query->bindValue(':search', $search, PDO::PARAM_INT);
$query->execute();
while ($row = $query->fetch(PDO::FETCH_OBJ)) {
// = $row['article'],'</br>';
$return = $row['article'];
}
return $return;
}
}
?>
This file is my test file, which is not that important just a testing-ground. Called test.php:
<?php
require_once('app_core/config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="/style/style.css" type="text/css" media="screen" />
</head>
<body>
<?php
$db = new DB();
//echo $db->db;
//echo $db->GetTestDB();
//$test = $db->TestThis();
//print_r($test);
echo $db->GetArticles('23');
?>
</body>
</html>
If it is possible I also have two other concerns: the first question is a matter of securit — is this a good practice or not? The other question is how do I hide files with this password data, so I can use them but no one can read them?
Ok, you’ve got a lot going on here, so I’ll try to address issues one at a time to make this class behave properly and in an object-oriented manner (instead of a collection of not-entirely-related methods).
First, your constructor:
Next your singleton accessor getInstance().
Since you have defined a method to access the class as a singleton, the
$dbproperty and the__construct()are madeprivate. At no point will you ever call$DB_class-instance = new DB()to instantiate it, or call$DB_class_instance->dbto access the connection directly. Instead, you will callDB::getInstance()to access the singleton instance and your methods likeGetArticles()to execute queries.Now onto your querying method:
Finally your controller code:
Since we made the class’
$dbpropertyprivate, it cannot be accessed outside the class. Therefore you would need to define querying methods similar toGetArticles()for any other queries you plan to run as well. If you think you will need to build ad-hoc queries that are not class methods sometimes, then you can change it toThen, you could do things outside the class like the following instead of having to build a class method to do it. You do still need to call
getInstance()however.Little style issue:
This won’t actually cause a problem since identifiers are case-insensitive, but stylistically it is weird.
define()is a function call and is usually used lowercase:About your file security
As long as your web server is correctly configured, no one else can read the files. Provided the web server sends .php files to the PHP interpreter rather than dumping their contents to the browser, the files are safe. If you are on a shared host and the host does not properly segregate your files from other tenants, that is their problem and the only good solution would be to get a better host.
It is wise, however, to store your sensitive files above your web server’s document root directory. Then even a misconfigured web server could not accidentally dump their contents to a client. They are only accessible to PHP via
include.