以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。<\/p>\n\n\n\n
<\/p>\n
本文實(shí)例介紹了PHP簡單投訴頁面的實(shí)現(xiàn)代碼,分享給大家供大家參考,具體內(nèi)容如下
php代碼:
<?php /* * 設(shè)計(jì)模式練習(xí) * 1.數(shù)據(jù)庫連接類(單例模式) * 2.調(diào)用接口實(shí)現(xiàn)留言本功能(工廠模式) * 3.實(shí)現(xiàn)分級(jí)舉報(bào)處理功能(責(zé)任鏈模式) * 4.發(fā)送不同組合的舉報(bào)信息(橋接模式) * 5.發(fā)送不同格式的舉報(bào)信息(適配器模式) * 6.在投訴內(nèi)容后自動(dòng)追加時(shí)間(裝飾器模式) * 7.根據(jù)會(huì)員登錄信息變換顯示風(fēng)格(觀察者模式) * 8.根據(jù)發(fā)帖長度加經(jīng)驗(yàn)值(策略模式) */ interface DB { function conn(); } /** * 單例模式 */ class MysqlSingle implements DB { protected static $_instance = NULL; public static function getInstance() { if (!self::$_instance instanceof self) { self::$_instance = new self; } return self::$_instance; } final protected function __construct() { echo 'Mysql單例創(chuàng)建成功<br>'; } final protected function __clone() { return false; } public function conn() { echo 'Mysql連接成功<br>'; } } /** * 工廠模式 */ interface Factory { function createDB(); } class MysqlFactory implements Factory { public function createDB() { echo 'Mysql工廠創(chuàng)建成功<br>'; return MysqlSingle::getInstance(); } } /** * 根據(jù)用戶名顯示不同風(fēng)格 * 觀察者模式 */ class Observer implements SplSubject { protected $_observers = NULL; public $_style = NULL; public function __construct($style) { $this->_style = $style; $this->_observers = new SplObjectStorage(); } public function show() { $this->notify(); } public function attach(SplObserver $observer) { $this->_observers->attach($observer); } public function detach(SplObserver $observer) { $this->_observers->detach($observer); } public function notify() { $this->_observers->rewind(); while ($this->_observers->valid()) { $observer = $this->_observers->current(); $observer->update($this); $this->_observers->next(); } } } class StyleA implements SplObserver { public function update(SplSubject $subject) { echo $subject->_style . ' 模塊A<br>'; } } class StyleB implements SplObserver { public function update(SplSubject $subject) { echo $subject->_style . ' 模塊B<br>'; } } /** * 根據(jù)不同方式進(jìn)行投訴 * 橋接模式 */ class Bridge { protected $_obj = NULL; public function __construct($obj) { $this->_obj = $obj; } public function msg($type) { } public function show() { $this->msg(); $this->_obj->msg(); } } class BridgeEmail extends Bridge { public function msg() { echo 'Email>>'; } } class BridgeSms extends Bridge { public function msg() { echo 'Sms>>'; } } class Normal { public function msg() { echo 'Normal<br>'; } } class Danger { public function msg() { echo 'Danger<br>'; } } /** * 適配器模式 */ class Serialize { public $content = NULL; public function __construct($content) { $this->content = serialize($content); } public function show() { return '序列化格式:<br>' . $this->content; } } class JsonAdapter extends Serialize { public function __construct($content) { parent::__construct($content); $tmp = unserialize($this->content); $this->content = json_encode($tmp, TRUE); } public function show() { return 'Json格式:<br>' . $this->content; } } /** * 在投訴內(nèi)容后自動(dòng)追加 * 裝飾器模式 */ class Base { protected $_content = NULL; public function __construct($content) { $this->_content = $content; } public function getContent() { return $this->_content; } } class Decorator { private $_base = NULL; public function __construct(Base $base) { $this->_base = $base; } public function show() { return $this->_base->getContent() . '>>系統(tǒng)時(shí)間:' . date('Y-m-d H:i:s', time()); } } /** * 分級(jí)舉報(bào)處理功能 * 責(zé)任鏈模式 */ class level1 { protected $_level = 1; protected $_top = 'Level2'; public function deal($level) { if ($level <= $this->_level) { echo '處理級(jí)別:1<br>'; return; } $top = new $this->_top; $top->deal($level); } } class level2 { protected $_level = 2; protected $_top = 'Level3'; public function deal($level) { if ($level <= $this->_level) { echo '處理級(jí)別:2<br>'; return; } $top = new $this->_top; $top->deal($level); } } class level3 { protected $_level = 3; protected $_top = 'Level2'; public function deal($level) { echo '處理級(jí)別:3<br>'; return; } } if (!empty($_POST)) { echo '<h1>PHP設(shè)計(jì)模式</h1>'; //連接數(shù)據(jù)庫——工廠+單例模式 $mysqlFactory = new MysqlFactory(); $single = $mysqlFactory->createDB(); $single->conn(); echo '<br>'; //觀察者模式 $username = $_POST['username']; $ob = new Observer($username); $a = new StyleA(); $ob->attach($a); $b = new StyleB(); $ob->attach($b); $ob->show(); echo '<br>'; $ob->detach($b); $ob->show(); echo '<br>'; //橋接模式 $typeM = $_POST['typeM']; $typeN = 'Bridge' . $_POST['typeN']; $obj = new $typeN(new $typeM); $obj->show(); echo '<br>'; //適配器模式 $post = $_POST; $obj = new Serialize($post); echo $obj->show(); echo '<br>'; $json = new JsonAdapter($post); echo $json->show(); echo '<br>'; echo '<br>'; //裝飾器模式 $content = $_POST['content']; $decorator = new Decorator(new Base($content)); echo $decorator->show(); echo '<br>'; //責(zé)任鏈模式 echo '<br>'; $level = $_POST['level']; $deal = new Level1(); $deal->deal(intval($level)); return; } require("0.html");
html代碼:
<!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>PHP設(shè)計(jì)模式</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> div{border:solid gray 1px;margin-top:10px;height: 100px;width: 200px;} </style> </head> <body> <form action="0.php" method="post"> <h1>用戶名</h1> <select id="username" name="username"> <option value="Tom">Tom</option> <option value="Lily">Lily</option> </select> <h1>投訴方式</h1> <select id="type" name="typeM"> <option value="Normal">Normal</option> <option value="Danger">Danger</option> </select> <select id="type" name="typeN"> <option value="Email">Email</option> <option value="Sms">Sms</option> </select> <h1>處理級(jí)別</h1> <select id="level" name="level"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <h1>投訴內(nèi)容</h1> <textarea id="content" name="content" rows="3"></textarea> <button type="submit">提交</button> </form> </body> </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
免費(fèi)脫衣圖片
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片
用於從照片中去除衣服的線上人工智慧工具。
AI脫衣器
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!
好用且免費(fèi)的程式碼編輯器
中文版,非常好用
強(qiáng)大的PHP整合開發(fā)環(huán)境
視覺化網(wǎng)頁開發(fā)工具
神級(jí)程式碼編輯軟體(SublimeText3)
在PHP中獲取當(dāng)前會(huì)話ID的方法是使用session_id()函數(shù),但必須先調(diào)用session_start()才能成功獲取。 1.調(diào)用session_start()啟動(dòng)會(huì)話;2.使用session_id()讀取會(huì)話ID,輸出類似abc123def456ghi789的字符串;3.若返回為空,檢查是否遺漏session_start()、用戶是否首次訪問或會(huì)話是否被銷毀;4.會(huì)話ID可用於日誌記錄、安全驗(yàn)證和跨請(qǐng)求通信,但需注意安全性。確保正確開啟會(huì)話後即可順利獲取ID。
要從PHP字符串中提取子字符串,可使用substr()函數(shù),其語法為substr(string$string,int$start,?int$length=null),若未指定長度則截取至末尾;處理多字節(jié)字符如中文時(shí)應(yīng)使用mb_substr()函數(shù)以避免亂碼;若需根據(jù)特定分隔符截取字符串,可使用explode()或結(jié)合strpos()與substr()實(shí)現(xiàn),例如提取文件名擴(kuò)展名或域名。
UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat
在PHP中,最常用的方法是使用explode()函數(shù)將字符串拆分為數(shù)組。該函數(shù)通過指定的分隔符將字符串分割成多個(gè)部分並返回?cái)?shù)組,語法為explode(separator,string,limit),其中separator為分隔符,string為原字符串,limit為可選參數(shù)控制最大分割數(shù)量。例如$str="apple,banana,orange";$arr=explode(",",$str);結(jié)果為["apple","bana
JavaScript的數(shù)據(jù)類型分為原始類型和引用類型。原始類型包括string、number、boolean、null、undefined和symbol,其值不可變且賦值時(shí)復(fù)制副本,因此互不影響;引用類型如對(duì)象、數(shù)組和函數(shù)存儲(chǔ)的是內(nèi)存地址,指向同一對(duì)象的變量會(huì)相互影響。判斷類型可用typeof和instanceof,但需注意typeofnull的歷史問題。理解這兩類差異有助於編寫更穩(wěn)定可靠的代碼。
std::chrono在C 中用於處理時(shí)間,包括獲取當(dāng)前時(shí)間、測(cè)量執(zhí)行時(shí)間、操作時(shí)間點(diǎn)與持續(xù)時(shí)間及格式化解析時(shí)間。 1.獲取當(dāng)前時(shí)間使用std::chrono::system_clock::now(),可轉(zhuǎn)換為可讀字符串但係統(tǒng)時(shí)鐘可能不單調(diào);2.測(cè)量執(zhí)行時(shí)間應(yīng)使用std::chrono::steady_clock以確保單調(diào)性,並通過duration_cast轉(zhuǎn)換為毫秒、秒等單位;3.時(shí)間點(diǎn)(time_point)和持續(xù)時(shí)間(duration)可相互操作,但需注意單位兼容性和時(shí)鐘紀(jì)元(epoch)
toAccessenvironmentVariablesInphp,useGetenv()或$ _envsuperglobal.1.getEnv('var_name')retievesSpecificvariable.2。 $ _ en v ['var_name'] accessesvariablesifvariables_orderInphp.iniincludes“ e” .setVariablesViaCliWithvar = vualitephpscript.php,inapach
exmallationalbindinginphpallowsstatic :: torefertotheclassInallyCallentimeInIminInheritancesCenarios.beForePhp5.3,self :: wallding referferenceedtheclassecceedtheclasswhereTheSheTheShodwhereTheShodWhereTheShodWhereTheShodWhereShodWhereShodWhereTheShodWhereShodeDwaseDined,causisionChildClass :: sayhello()sayhello()sayhello()sayhello()