国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

老生常談PHP面向?qū)ο笾钅J?必看篇)

2024-05-04 23:57:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這個(gè)模式主要由 命令類(lèi)、用戶(hù)請(qǐng)求數(shù)據(jù)類(lèi)、業(yè)務(wù)邏輯類(lèi)、命令類(lèi)工廠類(lèi)及調(diào)用類(lèi)構(gòu)成,各個(gè)類(lèi)的作用概括如下:

1、命令類(lèi):調(diào)用用戶(hù)請(qǐng)求數(shù)據(jù)類(lèi)和業(yè)務(wù)邏輯類(lèi);

2、用戶(hù)請(qǐng)求數(shù)據(jù)類(lèi):獲取用戶(hù)請(qǐng)求數(shù)據(jù)及保存后臺(tái)處理后返回的結(jié)果;

3、業(yè)務(wù)邏輯類(lèi):如以下的示例中驗(yàn)證用戶(hù)登陸信息是否正確的功能等;

4、命令工廠類(lèi)(我自己取的名字,哈哈):生成命令類(lèi)的實(shí)例,這個(gè)類(lèi)第一次看的時(shí)候我覺(jué)得有點(diǎn)屌,當(dāng)然看了幾遍了還是覺(jué)得很屌 :);

5、調(diào)用類(lèi):調(diào)用命令類(lèi),生成視圖;

直接看代碼:

//命令類(lèi)abstract class Command {  abstract function execute(CommandContext $context);}class LoginCommand extends Command{       //處理用戶(hù)登陸信息的命令類(lèi)  function execute (CommandCotext $context){    //CommandCotext 是一個(gè)處理用戶(hù)請(qǐng)求數(shù)據(jù)和后臺(tái)回饋數(shù)據(jù)的類(lèi)    $manager = Registry::getAccessManager();  //原文代碼中并沒(méi)有具體的實(shí)現(xiàn),但說(shuō)明了這是一個(gè)處理用戶(hù)登陸信息的業(yè)務(wù)邏輯類(lèi)    $user = $context->get('username');    $pass = $context->get('pass');    $user_obj = $manager->login($user,$pass);    if(is_null($user_obj)){      $context->setError($manager->getError);      return false;    }    $context->addParam('user',$user_obj);    return true;               //用戶(hù)登陸成功返回true  }}class FeedbackCommand extends Command{        //發(fā)送郵件的命令類(lèi)  function execute(CommandContext $context){    $msgSystem = Registry::getMessageSystem();    $email = $context->get('email');    $msg = $context->get('msg');    $topic = $context->get('topci');    $result = $msgSystem->send($email,$msg,$topic);    if(!$result){      $context->setError($msgSystem->getError());      return false;    }    return true;  }}//用戶(hù)請(qǐng)求數(shù)據(jù)類(lèi)  class CommandContext {  private $params = array();  private $error = '';  function __construct (){  $this->params = $_REQUEST;}function addParam($key,$val){  $this->params[$key] = $val;}function get($key){  return $this->params[$key];}function setError($error){  $this->error = $error;}function getError(){  return $this->error;}}//命令類(lèi)工廠,這個(gè)類(lèi)根據(jù)用戶(hù)請(qǐng)求數(shù)據(jù)中的action來(lái)生成命令類(lèi)class CommandNotFoundException extends Exception {}class CommandFactory {  private static $dir = 'commands';  static function getCommand($action='Default'){    if(preg_match('//w',$action)){      throw new Exception("illegal characters in action");    }    $class = UCFirst(strtolower($action))."Command";    $file = self::$dir.DIRECTORY_SEPARATOR."{$class}.php"; //DIRECTORY_SEPARATOR代表'/',這是一個(gè)命令類(lèi)文件的路徑    if(!file_exists($file)){      throw new CommandNotFoundException("could not find '$file'");    }    require_once($file);    if(!class_exists($class)){      throw new CommandNotFoundException("no '$class' class located");    }    $cmd = new $class();    return $cmd;  }}//調(diào)用者類(lèi),相當(dāng)于一個(gè)司令部它統(tǒng)籌所有的資源class Controller{  private $context;  function __construct(){    $this->context = new CommandContext();  //用戶(hù)請(qǐng)求數(shù)據(jù)  }  function getContext(){    return $this->context;  }  function process(){    $cmd = CommandFactory::getCommand($this->context->get('action'));    //通過(guò)命令工廠類(lèi)來(lái)獲取命令類(lèi)    if(!$comd->execute($this->context)){                            //處理失敗    } else {      //成功      // 分發(fā)視圖    }  }}// 客戶(hù)端$controller = new Controller();//偽造用戶(hù)請(qǐng)求,真實(shí)的場(chǎng)景中這些參數(shù)應(yīng)該是通過(guò)post或get的方式獲取的,貌似又廢話(huà)了:)$context = $controller->getContext();$context->addParam('action','login');$context->addParam('username','bob');$context->addParam('pass','tiddles');$controller->process();

以上這篇老生常談PHP面向?qū)ο笾钅J?必看篇)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 永顺县| 六盘水市| 无棣县| 乌鲁木齐县| 馆陶县| 周宁县| 新余市| 法库县| 宁河县| 合山市| 呈贡县| 衡东县| 中山市| 蓝山县| 贵溪市| 堆龙德庆县| 怀来县| 阜平县| 博爱县| 嘉善县| 巴林左旗| 吉安市| 新竹市| 濮阳县| 会理县| 深水埗区| 阿合奇县| 屯昌县| 北碚区| 宜良县| 绿春县| 马关县| 三穗县| 肇庆市| 龙泉市| 南木林县| 柳林县| 宣城市| 和平县| 南江县| 永宁县|