{"id":527,"date":"2010-10-04T16:17:54","date_gmt":"2010-10-04T16:17:54","guid":{"rendered":""},"modified":"2010-10-04T16:17:54","modified_gmt":"2010-10-04T16:17:54","slug":"527","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=527","title":{"rendered":"PHP \u7684 ArrayAccess \u63a5\u53e3&#8211;\u50cf\u6570\u7ec4\u4e00\u6837\u6765\u8bbf\u95ee\u4f60\u7684 PHP \u5bf9\u8c61"},"content":{"rendered":"<p>\u5982\u679c\u60f3\u8ba9\u5bf9\u8c61\u4f7f\u7528\u8d77\u6765\u50cf\u4e00\u4e2a PHP \u6570\u7ec4\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u5b9e\u73b0 ArrayAccess \u63a5\u53e3\uff1a<br \/><code><br \/>interface ArrayAccess<br \/>boolean offsetExists($index)<br \/>mixed offsetGet($index)<br \/>void offsetSet($index, $newvalue)<br \/>void offsetUnset($index)<br \/><\/code><\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8fd9\u4e2a\u63a5\u53e3\uff0c\u4f8b\u5b50\u5e76\u4e0d\u662f\u5b8c\u6574\u7684\uff0c\u4f46\u662f\u8db3\u591f\u770b\u61c2\uff0c:-&gt;<br \/><code><br \/>&lt;?php<br \/>class UserToSocialSecurity implements ArrayAccess<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;private $db;\/\/\u4e00\u4e2a\u5305\u542b\u7740\u6570\u636e\u5e93\u8bbf\u95ee\u65b9\u6cd5\u7684\u5bf9\u8c61<br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;function offsetExists($name)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $this-&gt;db-&gt;userExists($name);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;function offsetGet($name)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $this-&gt;db-&gt;getUserId($name);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;function offsetSet($name, $id)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;db-&gt;setUserId($name, $id);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;function offsetUnset($name)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;db-&gt;removeUser($name);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>}<\/p>\n<p>$userMap = new UserToSocialSecurity();<br \/>print \"John's ID number is \" . $userMap['John'];<br \/>?&gt;<br \/><\/code><\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u5f53 $userMap[\u2018John\u2019] \u67e5\u627e\u88ab\u6267\u884c\u65f6\uff0cPHP \u8c03\u7528\u4e86 offsetGet() \u65b9\u6cd5\uff0c\u7531\u8fd9\u4e2a\u65b9\u6cd5\u518d\u6765\u8c03\u7528\u6570\u636e\u5e93\u76f8\u5173\u7684 getUserId() \u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u60f3\u8ba9\u5bf9\u8c61\u4f7f\u7528\u8d77\u6765\u50cf\u4e00\u4e2a PHP \u6570\u7ec4\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u5b9e\u73b0 Arra&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=527"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/527\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}