{"id":2310,"date":"2015-05-15T15:09:36","date_gmt":"2015-05-15T07:09:36","guid":{"rendered":"http:\/\/www.huzs.top\/?p=2310"},"modified":"2015-05-15T15:10:11","modified_gmt":"2015-05-15T07:10:11","slug":"php%e4%b8%ad%e7%9a%84traits","status":"publish","type":"post","link":"https:\/\/www.huzs.top\/?p=2310","title":{"rendered":"PHP\u4e2d\u7684traits"},"content":{"rendered":"<div id=\"cnblogs_post_body\">\n<p>\u3000\u3000PHP 5.4\u4e2d\u7684traits\uff0c\u662f\u65b0\u5f15\u5165\u7684\u7279\u6027\uff0c\u4e2d\u6587\u8fd8\u771f\u4e0d\u77e5\u9053\u5982\u4f55\u51c6\u786e\u7ffb\u8bd1\u597d\u3002\u5176\u5b9e\u9645\u7684\u76ee\u7684\uff0c<strong>\u662f\u4e3a\u4e86\u6709\u7684\u573a\u5408\u60f3\u7528\u591a\u7ee7\u627f\uff0c\u4f46PHP\u53c8\u6ca1\u591a\u7ee7\u627f<\/strong>\uff0c\u4e8e\u662f\u5c31\u53d1\u660e\u4e86\u8fd9\u6837\u7684\u4e00\u4e2a\u4e1c\u897f\u3002<br \/><strong>Traits\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u7ec4\u80fd\u88ab\u4e0d\u540c\u7684\u7c7b\u90fd\u80fd\u8c03\u7528\u5230\u7684\u65b9\u6cd5\u96c6\u5408<\/strong>\uff0c\u4f46Traits\u4e0d\u662f\u7c7b\uff01\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002\u5148\u6765\u4f8b\u5b50\u770b\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<div class=\"cnblogs_code\"><\/p>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"http:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<p><\/p>\n<pre class=\"\">trait myTrait{<br>    function traitMethod1(){}<br>    function traitMethod2(){}<br><br>}<br><br>\/\/\u7136\u540e\u662f\u8c03\u7528\u8fd9\u4e2atraits,\u8bed\u6cd5\u4e3a\uff1a<br>class myClass{<br>    use myTrait;<br>}<br><br>\/\/\u8fd9\u6837\u5c31\u53ef\u4ee5\u901a\u8fc7use myTraits\uff0c\u8c03\u7528Traits\u4e2d\u7684\u65b9\u6cd5\u4e86\uff0c\u6bd4\u5982\uff1a<br>$obj = new myClass();<br>$obj-&gt; traitMethod1 ();<br>$obj-&gt; traitMethod2 (); <br>&gt;<\/pre>\n<p><\/p>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"http:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<p><\/div>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u63a2\u7a76\u4e0b\u4e3a\u4ec0\u4e48\u8981\u7528traits\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6bd4\u5982\u6709\u4e24\u4e2a\u7c7b\uff0c\u5206\u522b\u4e3abusiness\uff08\u5546\u52a1\u8005\uff09\u548cIndividual(\u4e2a\u4eba\uff09\uff0c\u5b83\u4eec \u90fd\u6709\u5730\u5740\u7684\u5c5e\u6027\uff0c\u4f20\u7edf\u7684\u505a\u6cd5\u662f\uff0c\u518d\u62bd\u8c61\u51fa\u4e00\u4e2a\u8fd9\u4e24\u4e2a\u7c7b\u90fd\u5171\u540c\u6709\u7279\u6027\u7684\u7236\u7c7b\uff0c\u6bd4\u5982client\uff0c\u5728client\u7c7b\u4e2d\u8bbe\u7f6e\u8bbf\u95ee\u5c5e\u6027 address,business\u548cindividual\u5206\u522b\u7ee7\u627f\u4e4b\uff0c\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\"><\/p>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"http:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<p><\/p>\n<pre>\/\/ Class Client  <br>class Client  {  <br>    private $address;  <br>    public getAddress() {  <br>        return $this-&gt;address;  <br>    }       <br>    public setAddress($address) {  <br>        $this-&gt;address = $address;    <br>    }  <br>}  <br>      <br>class Business extends Client{  <br>    \/\/\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528address\u5c5e\u6027  <br>}  <br><br>\/\/ Class Individual  <br>class Individual extends Client{  <br>\/\/\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528address\u5c5e\u6027  <br>}<\/pre>\n<p><\/p>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"http:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<p><\/div>\n<p>\u4f46\u5047\u5982\u53c8\u6709\u4e00\u4e2a\u53eborder\u7c7b\u7684\uff0c\u9700\u8981\u8bbf\u95ee\u540c\u6837\u7684\u5730\u5740\u5c5e\u6027\uff0c\u90a3\u600e\u4e48\u529e\u5462\uff1forder\u7c7b\u662f\u6ca1\u529e\u6cd5\u7ee7\u627fclient\u7c7b\u7684\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u4e0d\u7b26\u5408OOP\u7684\u539f\u5219\u3002\u8fd9\u4e2a\u65f6\u5019traits\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2atraits\uff0c\u7528\u6765\u5b9a\u4e49\u8fd9\u4e9b\u516c\u5171\u5c5e\u6027\u3002<\/p>\n<div class=\"cnblogs_code\"><\/p>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"http:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<p><\/p>\n<pre>\/\/ Trait Address<br>trait Address{<br>    private $address;<br>    public getAddress() {<br>        eturn $this-&gt;address;<br>    }<br>    public setAddress($address) {<br>        $this-&gt;address = $address;<br>    }<br>}<br>\/\/ Class Business<br>class Business{<br>    use Address;<br>    \/\/ \u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528address\u5c5e\u6027<br>}<br>\/\/ Class Individual<br>class Individual{<br>    use Address;<br>    \/\/\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528address\u5c5e\u6027<br>}<br>\/\/ Class Order<br>class Order{<br>    use Address;<br>    \/\/\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528address\u5c5e\u6027<br>}<\/pre>\n<p><\/p>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"http:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<p><\/div>\n<p>\u8fd9\u6837\u5c31\u65b9\u4fbf\u591a\u4e86\uff01<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u3000PHP 5.4\u4e2d\u7684traits\uff0c\u662f\u65b0\u5f15\u5165\u7684\u7279\u6027\uff0c\u4e2d\u6587\u8fd8\u771f\u4e0d\u77e5\u9053\u5982\u4f55\u51c6\u786e\u7ffb\u8bd1\u597d\u3002\u5176\u5b9e\u9645\u7684\u76ee\u7684\uff0c\u662f\u4e3a\u4e86\u6709\u7684\u573a\u5408 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-2310","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/posts\/2310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.huzs.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2310"}],"version-history":[{"count":2,"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/posts\/2310\/revisions"}],"predecessor-version":[{"id":2312,"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/posts\/2310\/revisions\/2312"}],"wp:attachment":[{"href":"https:\/\/www.huzs.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.huzs.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.huzs.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}