{"id":397,"date":"2019-02-22T14:42:05","date_gmt":"2019-02-22T06:42:05","guid":{"rendered":"https:\/\/fengyuwuzu.cn\/?p=397"},"modified":"2019-02-27T14:49:07","modified_gmt":"2019-02-27T06:49:07","slug":"php%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fengyuwuzu.cn\/?p=397","title":{"rendered":"\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f"},"content":{"rendered":"\r\n<h1 class=\"wp-block-heading\" id=\"\u7b56\u7565\u6a21\u5f0f\">\u7b56\u7565\u6a21\u5f0f<\/h1>\r\n\r\n\r\n\r\n<p>\u7b56\u7565\u6a21\u5f0f\u662f\u5bf9\u8c61\u7684\u884c\u4e3a\u6a21\u5f0f\uff0c\u7528\u610f\u662f\u5bf9\u4e00\u7ec4\u7b97\u6cd5\u7684\u5c01\u88c5\u3002\u52a8\u6001\u7684\u9009\u62e9\u9700\u8981\u7684\u7b97\u6cd5\u5e76\u4f7f\u7528\u3002<\/p>\r\n\r\n\r\n\r\n<p>\u7b56\u7565\u6a21\u5f0f\u6307\u7684\u662f\u7a0b\u5e8f\u4e2d\u6d89\u53ca\u51b3\u7b56\u63a7\u5236\u7684\u4e00\u79cd\u6a21\u5f0f\u3002\u7b56\u7565\u6a21\u5f0f\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u672c\u8eab\u7684\u6838\u5fc3\u601d\u60f3\u5c31\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u591a\u5f62\u6027\u601d\u60f3\u3002<\/p>\r\n\r\n\r\n\r\n<p>\u7b56\u7565\u6a21\u5f0f\u7684\u4e09\u4e2a\u89d2\u8272\uff1a<\/p>\r\n\r\n\r\n\r\n<p>1\uff0e\u62bd\u8c61\u7b56\u7565\u89d2\u8272<\/p>\r\n\r\n\r\n\r\n<p>2\uff0e\u5177\u4f53\u7b56\u7565\u89d2\u8272<\/p>\r\n\r\n\r\n\r\n<p>3\uff0e\u73af\u5883\u89d2\u8272\uff08\u5bf9\u62bd\u8c61\u7b56\u7565\u89d2\u8272\u7684\u5f15\u7528\uff09<\/p>\r\n\r\n\r\n\r\n<p>\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\r\n\r\n\r\n\r\n<p>1\uff0e\u5b9a\u4e49\u62bd\u8c61\u89d2\u8272\u7c7b\uff08\u5b9a\u4e49\u597d\u5404\u4e2a\u5b9e\u73b0\u7684\u5171\u540c\u62bd\u8c61\u65b9\u6cd5\uff09<\/p>\r\n\r\n\r\n\r\n<p>2\uff0e\u5b9a\u4e49\u5177\u4f53\u7b56\u7565\u7c7b\uff08\u5177\u4f53\u5b9e\u73b0\u7236\u7c7b\u7684\u5171\u540c\u65b9\u6cd5\uff09<\/p>\r\n\r\n\r\n\r\n<p>3\uff0e\u5b9a\u4e49\u73af\u5883\u89d2\u8272\u7c7b\uff08\u79c1\u6709\u5316\u7533\u660e\u62bd\u8c61\u89d2\u8272\u53d8\u91cf\uff0c\u91cd\u8f7d\u6784\u9020\u65b9\u6cd5\uff0c\u6267\u884c\u62bd\u8c61\u65b9\u6cd5\uff09<\/p>\r\n\r\n\r\n\r\n<p>\u5c31\u5728\u7f16\u7a0b\u9886\u57df\u4e4b\u5916\uff0c\u6709\u8bb8\u591a\u4f8b\u5b50\u662f\u5173\u4e8e\u7b56\u7565\u6a21\u5f0f\u7684\u3002\u4f8b\u5982\uff1a<\/p>\r\n\r\n\r\n\r\n<p>\u5982\u679c\u6211\u9700\u8981\u5728\u65e9\u6668\u4ece\u5bb6\u91cc\u51fa\u53d1\u53bb\u4e0a\u73ed\uff0c\u6211\u53ef\u4ee5\u6709\u51e0\u4e2a\u7b56\u7565\u8003\u8651\uff1a\u6211\u53ef\u4ee5\u4e58\u5750\u5730\u94c1\uff0c\u4e58\u5750\u516c\u4ea4\u8f66\uff0c\u8d70\u8def\u6216\u5176\u5b83\u7684\u9014\u5f84\u3002\u6bcf\u4e2a\u7b56\u7565\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f46\u662f\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u8d44\u6e90\u3002<\/p>\r\n\r\n\r\n\r\n<p>\u7b56\u7565\u6a21\u5f0f\u7684\u4ee3\u7801\u5b9e\u4f8b\uff1a<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n    abstract class baseAgent { \/\/\u62bd\u8c61\u7b56\u7565\u7c7b\r\n        abstract function PrintPage();\r\n    }\r\n    \/\/\u7528\u4e8e\u5ba2\u6237\u7aef\u662fIE\u65f6\u8c03\u7528\u7684\u7c7b\uff08\u73af\u5883\u89d2\u8272\uff09\r\n    class ieAgent extends baseAgent {\r\n        function PrintPage() {\r\n            return 'IE';\r\n        }\r\n    }\r\n    \/\/\u7528\u4e8e\u5ba2\u6237\u7aef\u4e0d\u662fIE\u65f6\u8c03\u7528\u7684\u7c7b\uff08\u73af\u5883\u89d2\u8272\uff09\r\n    class otherAgent extends baseAgent {\r\n        function PrintPage() {\r\n            return 'not IE';\r\n        }\r\n    }\r\n    class Browser { \/\/\u5177\u4f53\u7b56\u7565\u89d2\u8272\r\n        public function call($object) {\r\n                return $object-&gt;PrintPage ();\r\n            }\r\n        }\r\n        $bro = new Browser ();\r\n    echo $bro-&gt;call ( new ieAgent () );\r\n?&gt;<\/code><\/pre>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\" id=\"\u5de5\u5382\u6a21\u5f0f\">\u5de5\u5382\u6a21\u5f0f<\/h1>\r\n\r\n\r\n\r\n<p>\u5de5\u5382\u6a21\u5f0f\u662f\u6211\u4eec\u6700\u5e38\u7528\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\u6a21\u5f0f\uff0c\u662f\u7528\u5de5\u5382\u65b9\u6cd5\u4ee3\u66ffnew\u64cd\u4f5c\u7684\u4e00\u79cd\u6a21\u5f0f\u3002<\/p>\r\n\r\n\r\n\r\n<p>\u4f7f\u7528\u5de5\u5382\u6a21\u5f0f\u7684\u597d\u5904\u662f\uff0c\u5982\u679c\u4f60\u60f3\u8981\u66f4\u6539\u6240\u5b9e\u4f8b\u5316\u7684\u7c7b\u540d\u7b49\uff0c\u5219\u53ea\u9700\u66f4\u6539\u8be5\u5de5\u5382\u65b9\u6cd5\u5185\u5bb9\u5373\u53ef\uff0c\u4e0d\u9700\u9010\u4e00\u5bfb\u627e\u4ee3\u7801\u4e2d\u5177\u4f53\u5b9e\u4f8b\u5316\u7684\u5730\u65b9\uff08new\u5904\uff09\u4fee\u6539\u4e86\u3002\u4e3a\u7cfb\u7edf\u7ed3\u6784\u63d0\u4f9b\u7075\u6d3b\u7684\u52a8\u6001\u6269\u5c55\u673a\u5236\uff0c\u51cf\u5c11\u4e86\u8026\u5408\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nheader('Content-Type:text\/html;charset=utf-8');\r\n\/**\r\n *\u7b80\u5355\u5de5\u5382\u6a21\u5f0f\uff08\u9759\u6001\u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\uff09\r\n *\/\r\n\/**\r\n * Interface people \u4eba\u7c7b\r\n *\/\r\ninterface  people\r\n{\r\n    public function  say();\r\n}\r\n\/**\r\n * Class man \u7ee7\u627fpeople\u7684\u7537\u4eba\u7c7b\r\n *\/\r\nclass man implements people\r\n{\r\n    \/\/ \u5177\u4f53\u5b9e\u73b0people\u7684say\u65b9\u6cd5\r\n    public function say()\r\n    {\r\n        echo '\u6211\u662f\u7537\u4eba&lt;br&gt;';\r\n    }\r\n}\r\n\/**\r\n * Class women \u7ee7\u627fpeople\u7684\u5973\u4eba\u7c7b\r\n *\/\r\nclass women implements people\r\n{\r\n    \/\/ \u5177\u4f53\u5b9e\u73b0people\u7684say\u65b9\u6cd5\r\n    public function say()\r\n    {\r\n        echo '\u6211\u662f\u5973\u4eba&lt;br&gt;';\r\n    }\r\n}\r\n\/**\r\n * Class SimpleFactoty \u5de5\u5382\u7c7b\r\n *\/\r\nclass SimpleFactoty\r\n{\r\n    \/\/ \u7b80\u5355\u5de5\u5382\u91cc\u7684\u9759\u6001\u65b9\u6cd5-\u7528\u4e8e\u521b\u5efa\u7537\u4eba\u5bf9\u8c61\r\n    static function createMan()\r\n    {\r\n        return new man();\r\n    }\r\n    \/\/ \u7b80\u5355\u5de5\u5382\u91cc\u7684\u9759\u6001\u65b9\u6cd5-\u7528\u4e8e\u521b\u5efa\u5973\u4eba\u5bf9\u8c61\r\n    static function createWomen()\r\n    {\r\n        return new women();\r\n    }\r\n}\r\n\/**\r\n * \u5177\u4f53\u8c03\u7528\r\n *\/\r\n$man = SimpleFactoty::createMan();\r\n$man-&gt;say();\r\n$woman = SimpleFactoty::createWomen();\r\n$woman-&gt;say();<\/code><\/pre>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\" id=\"\u5355\u4f8b\u6a21\u5f0f\">\u5355\u4f8b\u6a21\u5f0f<\/h1>\r\n\r\n\r\n\r\n<p>\u5355\u4f8b\u6a21\u5f0f\u786e\u4fdd\u67d0\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u800c\u4e14\u81ea\u884c\u5b9e\u4f8b\u5316\u5e76\u5411\u6574\u4e2a\u7cfb\u7edf\u63d0\u4f9b\u8fd9\u4e2a\u5b9e\u4f8b\u3002<\/p>\r\n\r\n\r\n\r\n<p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5728\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\uff0c\u7ebf\u7a0b\u6c60\u3001\u7f13\u5b58\u3001\u65e5\u5fd7\u5bf9\u8c61\u3001\u5bf9\u8bdd\u6846\u3001\u6253\u5370\u673a\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u663e\u5361\u7684\u9a71\u52a8\u7a0b\u5e8f\u5e38\u88ab\u8bbe\u8ba1\u6210\u5355\u4f8b\u3002<\/p>\r\n\r\n\r\n\r\n<p>\u5355\u4f8b\u6a21\u5f0f\u52063\u79cd\uff1a\u61d2\u6c49\u5f0f\u5355\u4f8b\u3001\u997f\u6c49\u5f0f\u5355\u4f8b\u3001\u767b\u8bb0\u5f0f\u5355\u4f8b\u3002<\/p>\r\n\r\n\r\n\r\n<p>\u5355\u4f8b\u6a21\u5f0f\u6709\u4ee5\u4e0b3\u4e2a\u7279\u70b9\uff1a<\/p>\r\n\r\n\r\n\r\n<p>1\uff0e\u53ea\u80fd\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\r\n\r\n\r\n\r\n<p>2\uff0e\u5fc5\u987b\u81ea\u884c\u521b\u5efa\u8fd9\u4e2a\u5b9e\u4f8b\u3002<\/p>\r\n\r\n\r\n\r\n<p>3\uff0e\u5fc5\u987b\u7ed9\u5176\u4ed6\u5bf9\u8c61\u63d0\u4f9b\u8fd9\u4e00\u5b9e\u4f8b\u3002<\/p>\r\n\r\n\r\n\r\n<p>\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528PHP\u5355\u4f8b\u6a21\u5f0f\uff1f<\/p>\r\n\r\n\r\n\r\n<p>PHP\u4e00\u4e2a\u4e3b\u8981\u5e94\u7528\u573a\u5408\u5c31\u662f\u5e94\u7528\u7a0b\u5e8f\u4e0e\u6570\u636e\u5e93\u6253\u4ea4\u9053\u7684\u573a\u666f\uff0c\u5728\u4e00\u4e2a\u5e94\u7528\u4e2d\u4f1a\u5b58\u5728\u5927\u91cf\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u9488\u5bf9\u6570\u636e\u5e93\u53e5\u67c4\u8fde\u63a5\u6570\u636e\u5e93\u7684\u884c\u4e3a\uff0c\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u53ef\u4ee5\u907f\u514d\u5927\u91cf\u7684new\u64cd\u4f5c\u3002\u56e0\u4e3a\u6bcf\u4e00\u6b21new\u64cd\u4f5c\u90fd\u4f1a\u6d88\u8017\u7cfb\u7edf\u548c\u5185\u5b58\u7684\u8d44\u6e90\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>class Single {\r\n    private $name;\/\/\u58f0\u660e\u4e00\u4e2a\u79c1\u6709\u7684\u5b9e\u4f8b\u53d8\u91cf\r\n    private function __construct(){\/\/\u58f0\u660e\u79c1\u6709\u6784\u9020\u65b9\u6cd5\u4e3a\u4e86\u9632\u6b62\u5916\u90e8\u4ee3\u7801\u4f7f\u7528new\u6765\u521b\u5efa\u5bf9\u8c61\u3002\r\n    }\r\n    static public $instance;\/\/\u58f0\u660e\u4e00\u4e2a\u9759\u6001\u53d8\u91cf\uff08\u4fdd\u5b58\u5728\u7c7b\u4e2d\u552f\u4e00\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff09\r\n        static public function getinstance(){\/\/\u58f0\u660e\u4e00\u4e2agetinstance()\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u6709\u5b9e\u4f8b\u5bf9\u8c61\r\n        if(!self::$instance) self::$instance = new self();\r\n            return self::$instance;\r\n    }\r\n    public function setname($n){ $this-&gt;name = $n; }\r\n        public function getname(){ return $this-&gt;name; }\r\n}\r\n$oa = Single::getinstance();\r\n$ob = Single::getinstance();\r\n$oa-&gt;setname('hello world');\r\n$ob-&gt;setname('good morning');\r\necho $oa-&gt;getname();\/\/good morning\r\necho $ob-&gt;getname();\/\/good morning<\/code><\/pre>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\" id=\"\u6ce8\u518c\u6a21\u5f0f\">\u6ce8\u518c\u6a21\u5f0f<\/h1>\r\n\r\n\r\n\r\n<p>\u6ce8\u518c\u6a21\u5f0f\uff0c\u89e3\u51b3\u5168\u5c40\u5171\u4eab\u548c\u4ea4\u6362\u5bf9\u8c61\u3002\u5df2\u7ecf\u521b\u5efa\u597d\u7684\u5bf9\u8c61\uff0c\u6302\u5728\u5230\u67d0\u4e2a\u5168\u5c40\u53ef\u4ee5\u4f7f\u7528\u7684\u6570\u7ec4\u4e0a\uff0c\u5728\u9700\u8981\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u4ece\u8be5\u6570\u7ec4\u4e0a\u83b7\u53d6\u5373\u53ef\u3002\u5c06\u5bf9\u8c61\u6ce8\u518c\u5230\u5168\u5c40\u7684\u6811\u4e0a\u3002\u4efb\u4f55\u5730\u65b9\u76f4\u63a5\u53bb\u8bbf\u95ee\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nclass Register\r\n{\r\n    protected static  $objects;\r\n        function set($alias,$object)\/\/\u5c06\u5bf9\u8c61\u6ce8\u518c\u5230\u5168\u5c40\u7684\u6811\u4e0a\r\n        {\r\n            self::$objects[$alias]=$object;\/\/\u5c06\u5bf9\u8c61\u653e\u5230\u6811\u4e0a\r\n        }\r\n        static function get($name){\r\n        return self::$objects[$name];\/\/\u83b7\u53d6\u67d0\u4e2a\u6ce8\u518c\u5230\u6811\u4e0a\u7684\u5bf9\u8c61\r\n    }\r\n    function _unset($alias)\r\n\u3000\u3000{\r\n        unset(self::$objects[$alias]);\/\/\u79fb\u9664\u67d0\u4e2a\u6ce8\u518c\u5230\u6811\u4e0a\u7684\u5bf9\u8c61\u3002\r\n    }\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\" id=\"\u9002\u914d\u5668\u6a21\u5f0f\">\u9002\u914d\u5668\u6a21\u5f0f<\/h1>\r\n\r\n\r\n\r\n<p>\u5c06\u5404\u79cd\u622a\u7136\u4e0d\u540c\u7684\u51fd\u6570\u63a5\u53e3\u5c01\u88c5\u6210\u7edf\u4e00\u7684API\u3002\u00a0<br \/>PHP\u4e2d\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u6709MySQL,MySQLi,PDO\u4e09\u79cd\uff0c\u53ef\u4ee5\u7528\u9002\u914d\u5668\u6a21\u5f0f\u7edf\u4e00\u6210\u4e00\u81f4\uff0c\u4f7f\u4e0d\u540c\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u7edf\u4e00\u6210\u4e00\u6837\u7684API\u3002\u7c7b\u4f3c\u7684\u573a\u666f\u8fd8\u6709cache\u9002\u914d\u5668\uff0c\u53ef\u4ee5\u5c06memcache,redis,file,apc\u7b49\u4e0d\u540c\u7684\u7f13\u5b58\u51fd\u6570\uff0c\u7edf\u4e00\u6210\u4e00\u81f4\u3002\u00a0<br \/>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3(\u6709\u51e0\u4e2a\u65b9\u6cd5\uff0c\u4ee5\u53ca\u76f8\u5e94\u7684\u53c2\u6570)\u3002\u7136\u540e\uff0c\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u60c5\u51b5\uff0c\u5c31\u5199\u51e0\u4e2a\u7c7b\u5b9e\u73b0\u8be5\u63a5\u53e3\u3002\u5c06\u5b8c\u6210\u76f8\u4f3c\u529f\u80fd\u7684\u51fd\u6570\uff0c\u7edf\u4e00\u6210\u4e00\u81f4\u7684\u65b9\u6cd5\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\u63a5\u53e3 IDatabase\r\n&lt;?php\r\nnamespace IMooc;\r\ninterface IDatabase\r\n{\r\n    function connect($host, $user, $passwd, $dbname);\r\n    function query($sql);\r\n    function close();\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>MySQL<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nnamespace IMooc\\Database;\r\nuse IMooc\\IDatabase;\r\nclass MySQL implements IDatabase\r\n{\r\n    protected $conn;\r\n        function connect($host, $user, $passwd, $dbname)\r\n        {\r\n            $conn = mysql_connect($host, $user, $passwd);\r\n            mysql_select_db($dbname, $conn);\r\n            $this-&gt;conn = $conn;\r\n    }\r\n    function query($sql)\r\n        {\r\n            $res = mysql_query($sql, $this-&gt;conn);\r\n            return $res;\r\n    }\r\n    function close()\r\n    {\r\n        mysql_close($this-&gt;conn);\r\n    }\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>MySQLi<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nnamespace IMooc\\Database;\r\nuse IMooc\\IDatabase;\r\nclass MySQLi implements IDatabase\r\n{\r\n    protected $conn;\r\n    function connect($host, $user, $passwd, $dbname)\r\n    {\r\n        $conn = mysqli_connect($host, $user, $passwd, $dbname);\r\n        $this-&gt;conn = $conn;\r\n    }\r\n    function query($sql)\r\n    {\r\n        return mysqli_query($this-&gt;conn, $sql);\r\n    }\r\n    function close()\r\n    {\r\n        mysqli_close($this-&gt;conn);\r\n    }\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<h1 class=\"wp-block-heading\" id=\"\u89c2\u5bdf\u8005\u6a21\u5f0f\">\u89c2\u5bdf\u8005\u6a21\u5f0f<\/h1>\r\n\r\n\r\n\r\n<p>1\uff1a\u89c2\u5bdf\u8005\u6a21\u5f0f(Observer)\uff0c\u5f53\u4e00\u4e2a\u5bf9\u8c61\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u4f9d\u8d56\u5b83\u7684\u5bf9\u8c61\u5168\u90e8\u4f1a\u6536\u5230\u901a\u77e5\uff0c\u5e76\u81ea\u52a8\u66f4\u65b0\u3002\u00a0<br \/>2\uff1a\u573a\u666f:\u4e00\u4e2a\u4e8b\u4ef6\u53d1\u751f\u540e\uff0c\u8981\u6267\u884c\u4e00\u8fde\u4e32\u66f4\u65b0\u64cd\u4f5c\u3002\u4f20\u7edf\u7684\u7f16\u7a0b\u65b9\u5f0f\uff0c\u5c31\u662f\u5728\u4e8b\u4ef6\u7684\u4ee3\u7801\u4e4b\u540e\u76f4\u63a5\u52a0\u5165\u5904\u7406\u7684\u903b\u8f91\u3002\u5f53\u66f4\u65b0\u7684\u903b\u8f91\u589e\u591a\u4e4b\u540e\uff0c\u4ee3\u7801\u4f1a\u53d8\u5f97\u96be\u4ee5\u7ef4\u62a4\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f\u8026\u5408\u7684\uff0c\u4fb5\u5165\u5f0f\u7684\uff0c\u589e\u52a0\u65b0\u7684\u903b\u8f91\u9700\u8981\u4fee\u6539\u4e8b\u4ef6\u7684\u4e3b\u4f53\u4ee3\u7801\u3002\u00a0<br \/>3\uff1a\u89c2\u5bdf\u8005\u6a21\u5f0f\u5b9e\u73b0\u4e86\u4f4e\u8026\u5408\uff0c\u975e\u4fb5\u5165\u5f0f\u7684\u901a\u77e5\u4e0e\u66f4\u65b0\u673a\u5236\u3002\u00a0<br \/>\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6\u89e6\u53d1\u62bd\u8c61\u7c7b\u3002<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>EventGenerator.php\r\n&lt;?php\r\nrequire_once 'Loader.php';\r\nabstract class EventGenerator{\r\n    private $observers = array();\r\n        function addObserver(Observer $observer){\r\n        $this-&gt;observers[]=$observer;\r\n    }\r\n    function notify(){\r\n        foreach ($this-&gt;observers as $observer){\r\n            $observer-&gt;update();\r\n        }\r\n    }\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>\u5b9a\u4e49\u4e00\u4e2a\u89c2\u5bdf\u8005\u63a5\u53e3<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Observer.php\r\n&lt;?php\r\nrequire_once 'Loader.php';\r\ninterface Observer{\r\n    function update();\/\/\u8fd9\u91cc\u5c31\u662f\u5728\u4e8b\u4ef6\u53d1\u751f\u540e\u8981\u6267\u884c\u7684\u903b\u8f91\r\n}\r\n\/\/\u4e00\u4e2a\u5b9e\u73b0\u4e86EventGenerator\u62bd\u8c61\u7c7b\u7684\u7c7b\uff0c\u7528\u4e8e\u5177\u4f53\u5b9a\u4e49\u67d0\u4e2a\u53d1\u751f\u7684\u4e8b\u4ef6<\/code><\/pre>\r\n\r\n\r\n\r\n<p>\u5b9e\u73b0<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">require 'Loader.php';\r\nclass Event extends EventGenerator{\r\n    function triger(){\r\n        echo \"Event<br \/>\";\r\n    }\r\n}\r\nclass Observer1 implements Observer{\r\n    function update(){\r\n        echo \"\u903b\u8f911<br \/>\";\r\n    }\r\n}\r\nclass Observer2 implements Observer{\r\n    function update(){\r\n        echo \"\u903b\u8f912<br \/>\";\r\n    }\r\n}\r\n$event = new Event();\r\n$event-&gt;addObserver(new Observer1());\r\n$event-&gt;addObserver(new Observer2());\r\n$event-&gt;triger();\r\n$event-&gt;notify();<\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>\u7b56\u7565\u6a21\u5f0f \u7b56\u7565\u6a21\u5f0f\u662f\u5bf9\u8c61\u7684\u884c\u4e3a\u6a21\u5f0f\uff0c\u7528\u610f\u662f\u5bf9\u4e00\u7ec4\u7b97\u6cd5\u7684\u5c01\u88c5\u3002\u52a8\u6001\u7684\u9009\u62e9\u9700\u8981\u7684\u7b97\u6cd5\u5e76\u4f7f\u7528\u3002 \u7b56\u7565\u6a21\u5f0f\u6307\u7684\u662f\u7a0b\u5e8f\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":309,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,1],"tags":[6],"class_list":["post-397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-news","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f | \u98ce\u96e8\u65e0\u963bblog<\/title>\n<meta name=\"description\" content=\"php\u8bbe\u8ba1\u6a21\u5f0f,php\u8bbe\u8ba1,\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u51e0\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fengyuwuzu.cn\/?p=397\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f | \u98ce\u96e8\u65e0\u963bblog\" \/>\n<meta property=\"og:description\" content=\"php\u8bbe\u8ba1\u6a21\u5f0f,php\u8bbe\u8ba1,\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u51e0\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fengyuwuzu.cn\/?p=397\" \/>\n<meta property=\"og:site_name\" content=\"\u98ce\u96e8\u65e0\u963bblog\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-22T06:42:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-02-27T06:49:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fengyuwuzu.cn\/wp-content\/uploads\/2017\/12\/timg.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"500\" \/>\n\t<meta property=\"og:image:height\" content=\"326\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"\u98ce\u96e8\u65e0\u963b\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u98ce\u96e8\u65e0\u963b\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fengyuwuzu.cn\/?p=397\",\"url\":\"https:\/\/fengyuwuzu.cn\/?p=397\",\"name\":\"\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f | \u98ce\u96e8\u65e0\u963bblog\",\"isPartOf\":{\"@id\":\"https:\/\/fengyuwuzu.cn\/#website\"},\"datePublished\":\"2019-02-22T06:42:05+00:00\",\"dateModified\":\"2019-02-27T06:49:07+00:00\",\"author\":{\"@id\":\"https:\/\/fengyuwuzu.cn\/#\/schema\/person\/34cd01f899e76e2afadb37539a184c9f\"},\"description\":\"php\u8bbe\u8ba1\u6a21\u5f0f,php\u8bbe\u8ba1,\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u51e0\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\",\"breadcrumb\":{\"@id\":\"https:\/\/fengyuwuzu.cn\/?p=397#breadcrumb\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fengyuwuzu.cn\/?p=397\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fengyuwuzu.cn\/?p=397#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/fengyuwuzu.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/fengyuwuzu.cn\/#website\",\"url\":\"https:\/\/fengyuwuzu.cn\/\",\"name\":\"\u98ce\u96e8\u65e0\u963b\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/fengyuwuzu.cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/fengyuwuzu.cn\/#\/schema\/person\/34cd01f899e76e2afadb37539a184c9f\",\"name\":\"\u98ce\u96e8\u65e0\u963b\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-CN\",\"@id\":\"https:\/\/fengyuwuzu.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/68e14230863f4533c3453c3dbd7675e7620bfe2f24de6f40d8089dd5ad6a419b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/68e14230863f4533c3453c3dbd7675e7620bfe2f24de6f40d8089dd5ad6a419b?s=96&d=mm&r=g\",\"caption\":\"\u98ce\u96e8\u65e0\u963b\"},\"description\":\"\u4e00\u4e2aWEB\u5f00\u53d1\u5de5\u7a0b\u5e08\uff01\u76ee\u524d\u6b63\u5728\u6df1\u5733\u67d0\u516c\u53f8\u4efb\u804c\uff01\",\"sameAs\":[\"http:\/\/fengyuwuzu.cn.com\"],\"url\":\"https:\/\/fengyuwuzu.cn\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f | \u98ce\u96e8\u65e0\u963bblog","description":"php\u8bbe\u8ba1\u6a21\u5f0f,php\u8bbe\u8ba1,\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u51e0\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fengyuwuzu.cn\/?p=397","og_locale":"zh_CN","og_type":"article","og_title":"\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f | \u98ce\u96e8\u65e0\u963bblog","og_description":"php\u8bbe\u8ba1\u6a21\u5f0f,php\u8bbe\u8ba1,\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u51e0\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1","og_url":"https:\/\/fengyuwuzu.cn\/?p=397","og_site_name":"\u98ce\u96e8\u65e0\u963bblog","article_published_time":"2019-02-22T06:42:05+00:00","article_modified_time":"2019-02-27T06:49:07+00:00","og_image":[{"width":500,"height":326,"url":"https:\/\/fengyuwuzu.cn\/wp-content\/uploads\/2017\/12\/timg.jpg","type":"image\/jpeg"}],"author":"\u98ce\u96e8\u65e0\u963b","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u98ce\u96e8\u65e0\u963b","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2\u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/fengyuwuzu.cn\/?p=397","url":"https:\/\/fengyuwuzu.cn\/?p=397","name":"\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f | \u98ce\u96e8\u65e0\u963bblog","isPartOf":{"@id":"https:\/\/fengyuwuzu.cn\/#website"},"datePublished":"2019-02-22T06:42:05+00:00","dateModified":"2019-02-27T06:49:07+00:00","author":{"@id":"https:\/\/fengyuwuzu.cn\/#\/schema\/person\/34cd01f899e76e2afadb37539a184c9f"},"description":"php\u8bbe\u8ba1\u6a21\u5f0f,php\u8bbe\u8ba1,\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u51e0\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1,\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1","breadcrumb":{"@id":"https:\/\/fengyuwuzu.cn\/?p=397#breadcrumb"},"inLanguage":"zh-CN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fengyuwuzu.cn\/?p=397"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/fengyuwuzu.cn\/?p=397#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/fengyuwuzu.cn\/"},{"@type":"ListItem","position":2,"name":"\u4e94\u79cd\u5e38\u89c1\u7684php\u8bbe\u8ba1\u6a21\u5f0f"}]},{"@type":"WebSite","@id":"https:\/\/fengyuwuzu.cn\/#website","url":"https:\/\/fengyuwuzu.cn\/","name":"\u98ce\u96e8\u65e0\u963b","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fengyuwuzu.cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-CN"},{"@type":"Person","@id":"https:\/\/fengyuwuzu.cn\/#\/schema\/person\/34cd01f899e76e2afadb37539a184c9f","name":"\u98ce\u96e8\u65e0\u963b","image":{"@type":"ImageObject","inLanguage":"zh-CN","@id":"https:\/\/fengyuwuzu.cn\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/68e14230863f4533c3453c3dbd7675e7620bfe2f24de6f40d8089dd5ad6a419b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/68e14230863f4533c3453c3dbd7675e7620bfe2f24de6f40d8089dd5ad6a419b?s=96&d=mm&r=g","caption":"\u98ce\u96e8\u65e0\u963b"},"description":"\u4e00\u4e2aWEB\u5f00\u53d1\u5de5\u7a0b\u5e08\uff01\u76ee\u524d\u6b63\u5728\u6df1\u5733\u67d0\u516c\u53f8\u4efb\u804c\uff01","sameAs":["http:\/\/fengyuwuzu.cn.com"],"url":"https:\/\/fengyuwuzu.cn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=\/wp\/v2\/posts\/397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=397"}],"version-history":[{"count":13,"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=\/wp\/v2\/posts\/397\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=\/wp\/v2\/posts\/397\/revisions\/413"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=\/wp\/v2\/media\/309"}],"wp:attachment":[{"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fengyuwuzu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}