{"id":569,"title":"php\u8bbe\u8ba1\u6a21\u5f0f-\u6865\u63a5\u6a21\u5f0f\u53ca\u5e94\u7528\u573a\u666f\u4e3e\u4f8b","good":0,"bad":0,"hit":1673,"created_at":"2023-01-19 07:58:09","content":"
<?php<\/p>
\/**<\/p>
* \u6865\u63a5\u6a21\u5f0f<\/p>
* \u5e94\u7528\u573a\u666f\uff1ahttps:\/\/zhuanlan.zhihu.com\/p\/58903776<\/p>
* \u4f5c\u7528\uff1a\u5f53\u4ea7\u751f\u7c7b\u7684\u56e0\u7d20\u5206\u5e03\u5728\u4e8c\u7ef4\u5750\u6807\u7cfb\uff0c\u53ef\u4ee5\u5c06\u539f\u6765\u9700\u8981x*y\u4e2a\u7c7b\u51cf\u5c11\u4e3ax+y\u4e2a\u7c7b<\/p>
* \u4e3e\u4f8b\uff1a\u53d1\u6d88\u606f\uff1ax\u8f74(\u77ed\u4fe1\uff0cemail),y\u8f74\uff1a\u6d88\u606f\u7d27\u6025\u7a0b\u5e8f\uff08\u4e00\u822c\uff0c\u7d27\u6025\uff0c\u5341\u5206\u706b\u6025\uff09\uff0c\u5982\u679c\u6bcf\u4e2a\u7c7b\u578b\u90fd\u5199\u4e00\u4e2a\u7c7b\u5c31\u662f6\u4e2a\uff0c\u5982\u679c\u7528\u6865\u63a5\u6a21\u5f0f\u5c31\u53ea\u67095\u4e2a\uff0c\u7ef4\u5ea6\u6570\u636e\u8d8a\u5927\u4f18\u52bf\u8d8a\u660e\u663e<\/p>
*\/<\/p>
<\/p>
<\/p>
\/\/\u4fe1\u4ef6\u7c7b\u578b<\/p>
interface msg<\/p>
{<\/p>
public function send($to, $msg);<\/p>
}<\/p>
<\/p>
\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>
\u53d1email\u7ed9\u5c0f\u660e,\u666e\u901a:\u4e0a\u73ed\u4e86<\/span><\/p> class telMsg implements msg<\/p> {<\/p> public function send($to, $msg)<\/p> {<\/p> echo "\u53d1\u77ed\u4fe1\u7ed9{$to},{$msg}<br\/>";<\/p> }<\/p> }<\/p> class emailMsg implements msg<\/p> {<\/p> public function send($to, $msg)<\/p> {<\/p> echo "\u53d1email\u7ed9{$to},{$msg}<br\/>";<\/p> }<\/p> }<\/p> \/\/\u7d27\u6025\u7a0b\u5ea6<\/p> abstract class info<\/p> {<\/p> protected $msg = null;<\/p> public function __construct(msg $msg)<\/p> {<\/p> $this->msg = $msg;<\/p> }<\/p> abstract public function msg($content);<\/p> public function send($to, $content)<\/p> {<\/p> $this->msg->send($to, $this->msg($content));<\/p> }<\/p> }<\/p> class normalInfo extends info<\/p> {<\/p> public function msg($content)<\/p> {<\/p> return '\u666e\u901a:' . $content;<\/p> }<\/p> }<\/p> class urgentInfo extends info<\/p> {<\/p> public function msg($content)<\/p> {<\/p> return '\u7d27\u6025:' . $content;<\/p> }<\/p> }<\/p> class veryUrgentInfo extends info<\/p> {<\/p> public function msg($content)<\/p> {<\/p> return '\u5341\u4e07\u706b\u6025:' . $content;<\/p> }<\/p> }<\/p> $normal = new normalInfo(new emailMsg());<\/p> $normal->send('\u5c0f\u660e', '\u4e0a\u73ed\u4e86');<\/p>
<\/p>
<\/p>
<\/p>
<\/p>
<\/p>
<\/p>
<\/p>
<\/p>
<\/p>
<\/p>"}