PHP Dependency Injection
I was looking for dependency injection.
- Am I on something or completely disconnected?
- Is the code good or bad - dependency injection or not?
The below code is the basis for the CMS system
Right now, there is a table named "page_details" with all the web pages stored in it.
Directory / file structure
.htaccess
index.php
classes/Db.class.php
classes/Page.class.php
config/config.php
config/init.php
.htaccess
# Mod rewrite enabled.
Options +FollowSymLinks
RewriteEngine on
# ---- Rules ----
RewriteRule ^([A-Za-z0-9-_]+)\.html$ index.php?page=$1 [NC,L]
index.php
<?php require_once ('config/init.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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="imagetoolbar" content="no" />
<title></title>
<meta name="Description" content="" />
<meta name="Keywords" content="" />
<link href="/css/styles.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
<?php
$page = new Pages($db);
print_r($page->get_page($_GET['page']));
?>
</body>
</html>
Db.class.php
<?php
class Db
{
private $dbhost;
private $dbuser;
private $dbpassword;
private $dbname;
private $connection;
public $query;
function __construct($dbhost, $dbuser, $dbpassword, $dbname)
{
$this->dbhost = $dbhost;
$this->dbuser = $dbuser;
$this->dbpassword = $dbpassword;
$this->dbname = $dbname;
}
public function open_connection()
{
try
{
$this->connection = mysqli_connect($this->dbhost, $this->dbuser, $this->
dbpassword, $this->dbname);
}
catch (exception $e)
{
throw $e;
}
}
public function close($query)
{
try
{
mysqli_close($this->connection);
}
catch (exception $e)
{
throw $e;
}
}
public function query($query)
{
try
{
$this->open_connection();
$result = mysqli_query($this->connection, $query);
return $result;
}
catch (exception $e)
{
throw $e;
}
$this->close_connection();
}
public function fetchArray($query)
{
$row = mysqli_fetch_assoc($query);
return $row;
}
public function count_rows($query)
{
$row = mysqli_num_rows($query);
return $row;
}
public function rows_affected()
{
$row = mysqli_affected_rows($this->connection);
return $row;
}
public function created_id()
{
$row = mysqli_insert_id($this->connection);
return $row;
}
}
?>
Page.class.php
<?php
class Pages
{
private $db;
function __construct($db)
{
$this->db = $db;
}
function get_page($seo_url)
{
$sql = $this->db->query("SELECT * FROM page_details WHERE seo_url='$seo_url'");
$row = $this->db->fetchArray($sql);
return $row;
}
}
?>
config.php
<?php
$config = array();
$config['dbtype'] = 'mysqli';
$config['dbhost'] = 'localhost';
$config['dbname'] = 'name';
$config['dbuser'] = 'user';
$config['dbpassword'] = 'password';
$config['absolute_path'] = '/var/www/vhosts/example.com/httpdocs';
$config['website_root'] = 'http://www.example.com/';
$config['dummy'] = '';
?>
init.php
<?php
require_once ('config/config.php');
function __autoload($class_name)
{
require_once (''.$config['absolute_path'].'classes/' . $class_name . '.class.php');
}
$db = new Db($config['dbhost'], $config['dbuser'], $config['dbpassword'], $config['dbname']);
?>
source to share
I'm not sure how you expect to get __autoload
to be called (I don't see you calling new someclass
anywhere in your code), but at first glance using __autoload
to automatically include classes to be loaded is a good idea and you're using it correctly.
Some general comments on your code:
- I would use PDO instead of mysqli directly - it's somewhat safer (SQL injection wise), more future proof, and I think it's easier too.
- I would check that file
require
d exists before trying to claim it and throw a nice exception that can be caught by the application and reported in a nice way. - you probably want
print
your content, notprint_r
, but i think you are doing this for debugging.
source to share
Dependency Injection is DB -> Pages? Yes, it looks good. This seems like a sane approach.
Here's an idea of ββencapsulation (not DI): you might think what you get from the Pages class. For now, it just gives you the db table name. What if it was Page
? Instead of returning the $ row of data, it can own it. Then you can add methods Page
to to access these different columns of page data. This architecture will give you a place to put any code about the page. This will instead go directly to the $ string in the displayed code (now a string print_r
).
source to share