我一直在研究依賴注入。PHP中的依賴注入
- 我對某事或是完全不瞭解?
- 代碼是好還是壞 - 依賴注入與否?
下面的代碼是一個CMS系統的基礎
現在有一個叫做「page_details」與存儲在它的所有網頁表格。
目錄/文件結構
.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']);
?>
您需要轉義$ seo_url,這裏有一個sql注入。 – Kevin 2009-09-13 19:32:37
哎呀。謝謝。 – Cudos 2009-09-13 20:15:50