PHP 继承,组合,单模式,GUID,等混合实例

时间:2021-09-05 00:27:16
<?php
header("Content-type: text/html; charset=utf-8");
header('Access-Control-Allow-Origin:*');
$BiaDuak = '************';//定义变量 使用时 echo $BiaDuak;
define("accessKeyId","************");//定义常量 使用时 echo accessKeyId;
define("accessKeySecret","************");
define("endpoint","************");
//公共类
class DbHelp
{
private $servername="************.com:4065";
private $username="root" ;
private $password="w386161506";
private $dbname="allhelp_db";
public $conn;
private static $_instance;
public static function getInstance()
{
if(! (self::$_instance instanceof self) )
{
self::$_instance = new self(); }
return self::$_instance;
}
private function __construct(){ $this->conn=new mysqli($this->servername,$this->username,$this->password,$this->dbname);
}
private function __clone(){}
}
//公共函数
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function create_guid() {
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
$hyphen = chr();// "-"
$uuid =
substr($charid, , )
.substr($charid, , )
.substr($charid,, )
.substr($charid,, )
;
return $uuid;
}
function guid(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr();// "-"
$uuid = chr()// "{"
.substr($charid, , ).$hyphen
.substr($charid, , ).$hyphen
.substr($charid,, ).$hyphen
.substr($charid,, ).$hyphen
.substr($charid,,)
.chr();// "}"
return $uuid;
}
}
function createguid() {
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
$hyphen = chr();// "-"
$uuid =
substr($charid, , ).$hyphen
.substr($charid, , ).$hyphen
.substr($charid,, ).$hyphen
.substr($charid,, ).$hyphen
.substr($charid,,)
;
return $uuid;
}
//用于继承的基类
class DbHelpBase
{
public $servername;
public $username;
public $password;
public $dbname;
public $conn;
public function DbHelpBase(){
$this->servername="************:4065";
$this->username="root" ;
$this->password="w386161506";
$this->dbname="allhelp_db";
$this->conn=new mysqli($this->servername,$this->username,$this->password,$this->dbname);
}
public function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
public function create_guid() {
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
$hyphen = chr();// "-"
$uuid =
substr($charid, , )
.substr($charid, , )
.substr($charid,, )
.substr($charid,, )
;
return $uuid;
}
}
//用于组合的类
class DbHelpField
{
public $servername;
public $username;
public $password;
public $dbname;
public $conn;
private static $_instance;
public static function getInstance()
{
if(! (self::$_instance instanceof self) )
{
self::$_instance = new self(); }
return self::$_instance;
}
private function __construct(){
$this->servername="************:4065";
$this->username="root" ;
$this->password="w386161506";
$this->dbname="allhelp_db";
$this->conn=new mysqli($this->servername,$this->username,$this->password,$this->dbname);
}
private function __clone(){}
public function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
public function create_guid() {
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
$hyphen = chr();// "-"
$uuid =
substr($charid, , )
.substr($charid, , )
.substr($charid,, )
.substr($charid,, )
;
return $uuid;
}
}
?>
<?php
header("Content-type: text/html; charset=utf-8");
header('Content-Type: application/json');
header('Access-Control-Allow-Origin:*');
require "config.php";
class DataUser
{
private $conn;
public function DataUser()
{
$dbcontext=DbHelp::getInstance();
$this->conn=$dbcontext->conn;
if($this->conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
} }
public function Query_alluser()
{
$sql = "SELECT * FROM users";
$result = $this->conn->query($sql);
$data =array();
if ($result->num_rows > )
{
while($rowObject = $result->fetch_object())
{
$data[]=$rowObject;
}
$json = json_encode($data);//把数据转换为JSON数据.
echo "{".'"Comment"'.":".$json."}";
}
else
{
echo "0 results";
}
}
}
echo "直接使用config.php中的类";
$t=new DataUser();
$t->Query_alluser();
echo "\n"; class DataUser1 extends DbHelpBase
{
public function Query_alluser()
{
$sql = "SELECT * FROM users";
$result = $this->conn->query($sql);
$data =array();
if ($result->num_rows > )
{
while($rowObject = $result->fetch_object())
{
$data[]=$rowObject;
}
$json = json_encode($data);//把数据转换为JSON数据.
echo "{".'"Comment"'.":".$json."}";
}
else
{
echo "0 results";
}
}
}
echo "继承config.php中的类";
$tt=new DataUser1();
echo $tt->servername;
echo $tt->create_guid();
echo $tt->Query_alluser();
echo "\n";
class DataUser2
{
public $conn;
public $formDbHelp;
public function DataUser2()
{
$formDbHelp=DbHelpField::getInstance();
$this->conn= $formDbHelp->conn;
}
public function Query_alluser()
{
$sql = "SELECT * FROM users";
//$result = $this->formDbHelp->conn->query($sql);//PHP中好像不能三级访问古多定义一个$conn字段
$result = $this->conn->query($sql);
$data =array();
if ($result->num_rows > )
{
while($rowObject = $result->fetch_object())
{
$data[]=$rowObject;
}
$json = json_encode($data);//把数据转换为JSON数据.
echo "{".'"Comment"'.":".$json."}";
}
else
{
echo "0 results";
}
}
public function CreateGuid()
{
echo $this->formDbHelp->create_guid();//错误
}
}
echo "组合config.php中的类";
$ttt=new DataUser2();
echo $ttt->Query_alluser();
//echo $ttt->formDbHelp->create_guid();//错误
?>