{"id":818,"date":"2013-03-20T14:36:15","date_gmt":"2013-03-20T06:36:15","guid":{"rendered":"http:\/\/hesiwei.cn\/?p=818"},"modified":"2013-03-20T14:36:15","modified_gmt":"2013-03-20T06:36:15","slug":"php%e4%b8%adthisselfparent%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"http:\/\/hesiwei.cn\/?p=818","title":{"rendered":"PHP\u4e2dthis,self,parent\u7684\u533a\u522b"},"content":{"rendered":"<p>{\u4e00}PHP\u4e2dthis,self,parent\u7684\u533a\u522b\u4e4b\u4e00this\u7bc7<\/p>\n<p>      \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b(OOP,Object OrientedProgramming)\u73b0\u5df2\u7ecf\u6210\u4e3a\u7f16\u7a0b\u4eba\u5458\u7684\u4e00\u9879\u57fa\u672c\u6280\u80fd\u3002\u5229\u7528OOP\u7684\u601d\u60f3\u8fdb\u884cPHP\u7684\u9ad8\u7ea7\u7f16\u7a0b\uff0c\u5bf9\u4e8e\u63d0\u9ad8PHP\u7f16\u7a0b\u80fd\u529b\u548c\u89c4\u5212web\u5f00\u53d1\u6784\u67b6\u90fd\u662f\u5f88\u6709\u610f\u4e49\u7684\u3002<\/p>\n<p>PHP5\u7ecf\u8fc7\u91cd\u5199\u540e\uff0c\u5bf9OOP\u7684\u652f\u6301\u989d\u6709\u4e86\u5f88\u5927\u7684\u98de\u8dc3\uff0c\u6210\u4e3a\u4e86\u5177\u5907\u4e86\u5927\u90e8\u5206\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u7684\u7279\u6027\u7684\u8bed\u8a00\uff0c\u6bd4PHP4\u6709\u4e86\u5f88\u591a\u7684\u9762\u5411\u5bf9\u8c61\u7684\u7279\u6027\u3002\u8fd9\u91cc\u6211\u4e3b\u8981\u8c08\u7684\u662fthis,self,parent \u4e09\u4e2a\u5173\u952e\u5b57\u4e4b\u95f4\u7684\u533a\u522b\u3002\u4ece\u5b57\u9762\u4e0a\u6765\u7406\u89e3\uff0c\u5206\u522b\u662f\u6307\u8fd9\u3001\u81ea\u5df1\u3001\u7236\u4eb2\u3002\u5148\u521d\u6b65\u89e3\u91ca\u4e00\u4e0b\uff0cthis\u662f\u6307\u5411\u5f53\u524d\u5bf9\u8c61\u7684\u6307\u9488\uff08\u53ef\u4ee5\u770b\u6210C\u91cc\u9762\u7684\u6307\u9488\uff09\uff0cself\u662f\u6307\u5411\u5f53\u524d\u7c7b\u7684\u6307\u9488\uff0cparent\u662f\u6307\u5411\u7236\u7c7b\u7684\u6307\u9488\u3002\u6211\u4eec\u8fd9\u91cc\u9891\u7e41\u4f7f\u7528\u6307\u9488\u6765\u63cf\u8ff0\uff0c\u662f\u56e0\u4e3a\u6ca1\u6709\u66f4\u597d\u7684\u8bed\u8a00\u6765\u8868\u8fbe\u3002\u5173\u4e8e\u6307\u9488\u7684\u6982\u5ff5\uff0c\u5927\u5bb6\u53ef\u4ee5\u53bb\u53c2\u8003\u767e\u79d1\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u5c31\u6839\u636e\u5b9e\u9645\u7684\u4f8b\u5b50\u7ed3\u5408\u6765\u8bb2\u8bb2\u3002<br \/>\n<!--more--><br \/>\n< ?php\n\n classname          \/\/\u5efa\u7acb\u4e86\u4e00\u4e2a\u540d\u4e3aname\u7684\u7c7b\n {\n    private$name;         \/\/\u5b9a\u4e49\u5c5e\u6027\uff0c\u79c1\u6709\n\n    \/\/\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u8d4b\u503c\n    function __construct( $name )\n    {\n         $this->name =$name;         \/\/\u8fd9\u91cc\u5df2\u7ecf\u4f7f\u7528\u4e86this\u6307\u9488\u8bed\u53e5\u2460<br \/>\n    }<\/p>\n<p>    \/\/\u6790\u6784\u51fd\u6570<br \/>\n    function __destruct(){}<\/p>\n<p>    \/\/\u6253\u5370\u7528\u6237\u540d\u6210\u5458\u51fd\u6570<br \/>\n    function printname()<br \/>\n    {<br \/>\n         print( $this->name);             \/\/\u518d\u6b21\u4f7f\u7528\u4e86this\u6307\u9488\u8bed\u53e5\u2461\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528echo\u8f93\u51fa<br \/>\n    }<br \/>\n }<br \/>\n $obj1 = new name(&#8220;PBPHome&#8221;);   \/\/\u5b9e\u4f8b\u5316\u5bf9\u8c61 \u8bed\u53e5\u2462<\/p>\n<p> \/\/\u6267\u884c\u6253\u5370<br \/>\n $obj1->printname(); \/\/\u8f93\u51fa:PBPHome<br \/>\n echo&#8221;<br \/>&#8220;;                                    \/\/\u8f93\u51fa\uff1a\u56de\u8f66<\/p>\n<p> \/\/\u7b2c\u4e8c\u6b21\u5b9e\u4f8b\u5316\u5bf9\u8c61<br \/>\n $obj2 = new name( &#8220;PHP&#8221; );<\/p>\n<p> \/\/\u6267\u884c\u6253\u5370<br \/>\n $obj2->printname();                         \/\/\u8f93\u51fa\uff1aPHP<br \/>\n ?><\/p>\n<p>\u8bf4\u660e\uff1a\u4e0a\u9762\u7684\u7c7b\u5206\u522b\u5728 \u8bed\u53e5\u2460\u548c\u8bed\u53e5\u2461\u4f7f\u7528\u4e86this\u6307\u9488\uff0c\u90a3\u4e48\u5f53\u65f6this\u662f\u6307\u5411\u8c01\u5462\uff1f\u5176\u5b9ethis\u662f\u5728\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\u6765\u786e\u5b9a\u6307\u5411\u8c01\uff0c\u6bd4\u5982\u7b2c\u4e00\u6b21\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u65f6\u5019(\u8bed\u53e5\u2462)\uff0c\u90a3\u4e48\u5f53\u65f6this\u5c31\u662f\u6307\u5411$obj1\u5bf9\u8c61\uff0c\u90a3\u4e48\u6267\u884c\u8bed\u53e5\u2461\u7684\u6253\u5370\u65f6\u5c31\u628aprint( $this-><name ) \u53d8\u6210\u4e86 print($obj1t->name )\uff0c\u90a3\u4e48\u5f53\u7136\u5c31\u8f93\u51fa\u4e86&#8221;PBPHome&#8221;\u3002\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b\u7684\u65f6\u5019\uff0cprint($this->name )\u53d8\u6210\u4e86print( $obj2->name)\uff0c\u4e8e\u662f\u5c31\u8f93\u51fa\u4e86&#8221;PHP&#8221;\u3002\u6240\u4ee5\u8bf4\uff0cthis\u5c31\u662f\u6307\u5411\u5f53\u524d\u5bf9\u8c61\u5b9e\u4f8b\u7684\u6307\u9488\uff0c\u4e0d\u6307\u5411\u4efb\u4f55\u5176\u4ed6\u5bf9\u8c61\u6216\u7c7b\u3002<\/p>\n<p>{\u4e8c}\u3002PHP\u4e2dthis,self,parent\u7684\u533a\u522b\u4e4b\u4e8cself\u7bc7<\/p>\n<p>\u6b64\u7bc7\u6211\u4eec\u5c31self\u7684\u7528\u6cd5\u8fdb\u884c\u8bb2\u89e3<\/p>\n<p>\u9996\u5148\u6211\u4eec\u8981\u660e\u786e\u4e00\u70b9\uff0cself\u662f\u6307\u5411\u7c7b\u672c\u8eab\uff0c\u4e5f\u5c31\u662fself\u662f\u4e0d\u6307\u5411\u4efb\u4f55\u5df2\u7ecf\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\uff0c\u4e00\u822cself\u4f7f\u7528\u6765\u6307\u5411\u7c7b\u4e2d\u7684\u9759\u6001\u53d8\u91cf\u3002\u5047\u5982\u6211\u4eec\u4f7f\u7528\u7c7b\u91cc\u9762\u9759\u6001\uff08\u4e00\u822c\u7528\u5173\u952e\u5b57static\uff09\u7684\u6210\u5458\uff0c\u6211\u4eec\u4e5f\u5fc5\u987b\u4f7f\u7528self\u6765\u8c03\u7528\u3002\u8fd8\u8981\u6ce8\u610f\u4f7f\u7528self\u6765\u8c03\u7528\u9759\u6001\u53d8\u91cf\u5fc5\u987b\u4f7f\u7528:: (\u57df\u8fd0\u7b97\u7b26\u53f7)\uff0c\u89c1\u5b9e\u4f8b\u3002<\/p>\n<p> < ?php\n\n    classcounter     \/\/\u5b9a\u4e49\u4e00\u4e2acounter\u7684\u7c7b\n    {\n        \/\/\u5b9a\u4e49\u5c5e\u6027\uff0c\u5305\u62ec\u4e00\u4e2a\u9759\u6001\u53d8\u91cf$firstCount\uff0c\u5e76\u8d4b\u521d\u503c0 \u8bed\u53e5\u2460  \n        private static $firstCount = 0;\n        private $lastCount;\n\n        \/\/\u6784\u9020\u51fd\u6570\n        function __construct()\n        {\n             $this->lastCount =++self::$firstCount;      \/\/\u4f7f\u7528self\u6765\u8c03\u7528\u9759\u6001\u53d8\u91cf \u8bed\u53e5\u2461<br \/>\n        }<\/p>\n<p>        \/\/\u6253\u5370lastCount\u6570\u503c<br \/>\n        function printLastCount()<br \/>\n        {<br \/>\n             print( $this->lastCount );<br \/>\n        }<br \/>\n    }<\/p>\n<p> \/\/\u5b9e\u4f8b\u5316\u5bf9\u8c61<br \/>\n  $obj = new Counter();<\/p>\n<p> $obj->printLastCount();                             \/\/\u6267\u884c\u5230\u8fd9\u91cc\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u8f93\u51fa1<\/p>\n<p> ?><\/p>\n<p>\u8fd9\u91cc\u8981\u6ce8\u610f\u4e24\u4e2a\u5730\u65b9\u8bed\u53e5\u2460\u548c\u8bed\u53e5\u2461\u3002\u6211\u4eec\u5728\u8bed\u53e5\u2460\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9759\u6001\u53d8\u91cf$firstCount\uff0c\u90a3\u4e48\u5728\u8bed\u53e5\u2461\u7684\u65f6\u5019\u4f7f\u7528\u4e86self\u8c03\u7528\u8fd9\u4e2a\u503c\uff0c\u90a3\u4e48\u8fd9\u65f6\u5019\u6211\u4eec\u8c03\u7528\u7684\u5c31\u662f\u7c7b\u81ea\u5df1\u5b9a\u4e49\u7684\u9759\u6001\u53d8\u91cf$frestCount\u3002\u6211\u4eec\u7684\u9759\u6001\u53d8\u91cf\u4e0e\u4e0b\u9762\u5bf9\u8c61\u7684\u5b9e\u4f8b\u65e0\u5173\uff0c\u5b83\u53ea\u662f\u8ddf\u7c7b\u6709\u5173\uff0c\u90a3\u4e48\u6211\u8c03\u7528\u7c7b\u672c\u8eab\u7684\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u65e0\u6cd5\u4f7f\u7528this\u6765\u5f15\u7528\uff0c\u56e0\u4e3aself\u662f\u6307\u5411\u7c7b\u672c\u8eab\uff0c\u4e0e\u4efb\u4f55\u5bf9\u8c61\u5b9e\u4f8b\u65e0\u5173\u3002\u7136\u540e\u524d\u9762\u4f7f\u7528\u7684this\u8c03\u7528\u7684\u662f\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61$obj\uff0c\u5927\u5bb6\u4e0d\u8981\u6df7\u6dc6\u4e86\u3002<\/p>\n<p>\u5173\u4e8eself\u5c31\u8bf4\u5230\u8fd9\u91cc\uff0c\u7ed3\u5408\u4f8b\u5b50\u8fd8\u662f\u6bd4\u8f83\u65b9\u4fbf\u7406\u89e3\u7684\u3002\u7b2c\u4e8c\u7bc7\u7ed3\u675f\u3002<\/p>\n<p>{\u4e09}PHP\u4e2dthis,self,parent\u7684\u533a\u522b\u4e4b\u4e09parent\u7bc7<\/p>\n<p>\u6b64\u7bc7\u6211\u4eec\u5c31parent\u7684\u7528\u6cd5\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u660e\u786e\uff0cparent\u662f\u6307\u5411\u7236\u7c7b\u7684\u6307\u9488\uff0c\u4e00\u822c\u6211\u4eec\u4f7f\u7528parent\u6765\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>< ?php\n \/\/\u5efa\u7acb\u57fa\u7c7bAnimal\n class Animal\n {\n    public $name; \/\/\u57fa\u7c7b\u7684\u5c5e\u6027\uff0c\u540d\u5b57$name\n\n    \/\/\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u521d\u59cb\u5316\u8d4b\u503c\n    public function __construct( $name )\n    {\n         $this->name = $name;<br \/>\n    }<br \/>\n }<\/p>\n<p> \/\/\u5b9a\u4e49\u6d3e\u751f\u7c7bPerson \u7ee7\u627f\u81eaAnimal\u7c7b<br \/>\n class Person extends Animal<br \/>\n {<br \/>\n    public$personSex;       \/\/\u5bf9\u4e8e\u6d3e\u751f\u7c7b\uff0c\u65b0\u5b9a\u4e49\u4e86\u5c5e\u6027$personSex\u6027\u522b\u3001$personAge\u5e74\u9f84<br \/>\n    public $personAge;<\/p>\n<p>    \/\/\u6d3e\u751f\u7c7b\u7684\u6784\u9020\u51fd\u6570<br \/>\n    function __construct( $personSex, $personAge )<br \/>\n    {<br \/>\n         parent::__construct( &#8220;PBPHome&#8221;);    \/\/\u4f7f\u7528parent\u8c03\u7528\u4e86\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570 \u8bed\u53e5\u2460<br \/>\n         $this->personSex = $personSex;<br \/>\n         $this->personAge = $personAge;<br \/>\n    }<\/p>\n<p>    \/\/\u6d3e\u751f\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u7528\u4e8e\u6253\u5370\uff0c\u683c\u5f0f\uff1a\u540d\u5b57 is name,age is \u5e74\u9f84<br \/>\n    function printPerson()<br \/>\n    {<br \/>\n         print( $this->name. &#8221; is &#8220;.$this->personSex. &#8220;,age is &#8220;.$this->personAge );<br \/>\n     }<br \/>\n }<\/p>\n<p> \/\/\u5b9e\u4f8b\u5316Person\u5bf9\u8c61<br \/>\n $personObject = new Person( &#8220;male&#8221;, &#8220;21&#8221;);<\/p>\n<p> \/\/\u6267\u884c\u6253\u5370<br \/>\n $personObject->printPerson();\/\/\u8f93\u51fa\u7ed3\u679c\uff1aPBPHome is male,age is 21<\/p>\n<p> ?><\/p>\n<p>\u91cc\u9762\u540c\u6837\u542b\u6709this\u7684\u7528\u6cd5\uff0c\u5927\u5bb6\u81ea\u5df1\u5206\u6790\u3002\u6211\u4eec\u6ce8\u610f\u8fd9\u4e48\u4e2a\u7ec6\u8282\uff1a\u6210\u5458\u5c5e\u6027\u90fd\u662fpublic\uff08\u516c\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u7c7b\u5185\u90e8\u548c\u5916\u90e8\u7684\u4ee3\u7801\u5747\u53ef\u8bbf\u95ee\uff09\u7684\uff0c\u7279\u522b\u662f\u7236\u7c7b\u7684\uff0c\u8fd9\u662f\u4e3a\u4e86\u4f9b\u7ee7\u627f\u7c7b\u901a\u8fc7this\u6765\u8bbf\u95ee\u3002\u5173\u952e\u70b9\u5728\u8bed\u53e5\u2460\uff1aparent::__construct( &#8220;heiyeluren&#8221;)\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u4f7f\u7528parent\u6765\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u5bf9\u7236\u7c7b\u7684\u521d\u59cb\u5316\uff0c\u8fd9\u6837\uff0c\u7ee7\u627f\u7c7b\u7684\u5bf9\u8c61\u5c31\u90fd\u7ed9\u8d4b\u503c\u4e86name\u4e3aPBPHome\u3002\u6211\u4eec\u53ef\u4ee5\u6d4b\u8bd5\u4e0b\uff0c\u518d\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61$personObject1\uff0c\u6267\u884c\u6253\u5370\u540ename\u4ecd\u7136\u662fPBPHome\u3002<\/p>\n<p>\u603b\u7ed3\uff1athis\u662f\u6307\u5411\u5bf9\u8c61\u5b9e\u4f8b\u7684\u4e00\u4e2a\u6307\u9488\uff0c\u5728\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\u6765\u786e\u5b9a\u6307\u5411\uff1bself\u662f\u5bf9\u7c7b\u672c\u8eab\u7684\u4e00\u4e2a\u5f15\u7528\uff0c\u4e00\u822c\u7528\u6765\u6307\u5411\u7c7b\u4e2d\u7684\u9759\u6001\u53d8\u91cf\uff1bparent\u662f\u5bf9\u7236\u7c7b\u7684\u5f15\u7528\uff0c\u4e00\u822c\u4f7f\u7528parent\u6765\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002<br \/>\n\uff08\u8f6c\uff09<\/name><\/p>\n","protected":false},"excerpt":{"rendered":"<p>{\u4e00}PHP\u4e2dthis,self,parent\u7684\u533a\u522b\u4e4b\u4e00this\u7bc7 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b(OOP,Object Ori [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-818","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=818"}],"version-history":[{"count":0,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts\/818\/revisions"}],"wp:attachment":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}