####代理模式* 組成* 抽象角色:通過介面或抽象類別生命真實角色實現的業務方法* 代理角色:實現抽象角色,是真是角色的代理* 真實角色:實現抽象角色,定義真實角色所要實現的商務邏輯#####實現Mysql串連類```/** * Created by PhpStorm. * User: gewenrui * Date: 16/3/13 * Time: 上午8:16 */namespace db;//php允許借口儲存常量interface IConnectInfo{ //伺服器名稱 const HOST = "localhost"; //使用者名稱 const USERNAME = "root"; //密碼 const PASSWORD = ""; //資料庫名稱444444444444444444444 const DBNAME ="proxylog"; public function doConnect();}class UniversalConnect implements IConnectInfo{ //實現了範圍解析操作符來訪問串連值 //靜態變數處理速度優勢,提供私人可見度提供的封裝性 private static $server = IConnectInfo::HOST; private static $currentDB = IConnectInfo::DBNAME; private static $username = IConnectInfo::USERNAME; private static $password = IConnectInfo::PASSWORD; private static $hookup; public function doConnect() { //串連資料庫 self::$hookup = mysqli_connect(self::$server,self::$username,self::$password,self::$currentDB); if(self::$hookup){ echo "Successfucl connection to MYSQL"."
"; }else if(mysqli_connect_error(self::$hookup)){ echo "".mysqli_connect_error(); } return self::$hookup; }}```######進行建立表操作```class CreateTable{ //設定表名 private $tableMaster = "proxyLog"; private $hookup; public function __construct() { $this->tableMaster = "proxyLog"; $this->hookup = UniversalConnect::doConnect(); $drop = "DROP TABLE IF EXITS $this->tableMaster"."
"; //echo $drop; if($this->hookup->query($drop) == true){ printf("Old table %s has been dropped"."
",$this->tableMaster); } $sql = "CREATE TABLE $this->tableMaster (uname VARCHAR(15),pw VARCHAR(120))"; //echo $sql; if($this->hookup->query($sql) == true){ echo "Table{$this->tableMaster} has been created successful"; } $this->hookup->close(); }}```######向表中添加資料```class HashRegister{ public function __construct() { $this->tableMaster = "proxyLog"; $this->hookup = UniversalConnect::doConnect(); $username = $this->hookup->real_escape_string(trim($_POST['uname'])); $password = $this->hookup->real_escape_string(trim($_POST['pw'])); $sql = "INSERT INTO $this->tableMaster(uname,pw) VALUES('$username',md5('$password'))"; echo $sql; if($this->hookup->query($sql)){ echo "registeration conpleted"; }else if($result = $this->hookup->query($sql) == false){ printf("Invalid query : %s while query %s"); exit(); $this->hookup->close(); } }}```#####代理模式實現使用者登陸```tableMaster = "proxyLog"; $this->hookup = \db\UniversalConnect::doConnect(); $this->un = $this->hookup->real_escape_string(trim($_POST['uname'])); $this->pw = $this->hookup->real_escape_string(trim($_POST['pw'])); //代理模式 $this->getIface($this->proxy = new Proxy()); } public function getIface(ISubject $proxy){ $proxy->login($this->un,$this->pw); }}//代理類class Proxy implements ISubject{ private $tableMaster; private $hookup; private $logGood; private $realSubject; public function login($uNow,$pwNow){ $uname = $uNow; $pw = md5($pwNow); $this->logGood = false; $this->tableMaster = "proxyLog"; $this->hookup = \db\UniversalConnect::doConnect(); $sql = "SELECT pw from $this->tableMaster WHERE uname = '$uname'"; if($result = $this->hookup->query($sql)){ $row = $result->fetch_array(MYSQLI_ASSOC); echo $pw; if($row['pw'] == $pw){ $this->logGood = true; } $result->close(); }else if(($result = $this->hookup->query($sql)===false)){ printf("failed %s",$this->hookup->error); exit(); } $this->hookup->close(); if($this->logGood){ //執行request方法 $this->request(); }else{ echo "username and/or password not on record"; } } public function request() { $this->realSubject = new RealSubject(); $this->realSubject ->request(); }}class RealSubject implements ISubject{ public function request() { $practice = << data
- fuck u silly b
REQUEST;echo $practice; }}$data = new Client();```