{"id":93,"date":"2008-02-23T11:35:02","date_gmt":"2008-02-23T16:35:02","guid":{"rendered":"http:\/\/blogs.n1zyy.com\/andrew\/2008\/02\/23\/across-an-instance\/"},"modified":"2008-02-23T11:35:02","modified_gmt":"2008-02-23T16:35:02","slug":"across-an-instance","status":"publish","type":"post","link":"https:\/\/blogs.n1zyy.com\/andrew\/2008\/02\/23\/across-an-instance\/","title":{"rendered":"Across an Instance"},"content":{"rendered":"<p>Here&#8217;s the quick PHP tip of the day: class methods can access the protected (of any shared ancestors) and private (of the same type) members of <i>any<\/i> instance, not only their own instance. That may sound confusing, but it&#8217;s really not so much.<\/p>\n<p><!--more--><\/p>\n<p>Maybe some example code will explain it better. Let&#8217;s say I have a class that&#8217;s a counter, just wrapping an integer.<\/p>\n<pre><code>\n&lt;?php\nclass Counter\n{\n    protected $count = 0;\n    \n    public function increment()\n    {\n        $this-&gt;count++;\n    }\n\n    public function getCount()\n    {\n        return $this-&gt;count;\n    }\n}\n\n?&gt;\n<\/code><\/pre>\n<p>And now, for some reason, I want to add two counters together. I add a method to <code>Counter<\/code> named <code>add()<\/code> that accepts another instance of <code>Counter<\/code>. But rather than calling <code>increment()<\/code> <code>$count<\/code> times, I can simply write:<\/p>\n<pre><code>\n    public function add(Counter $c)\n    {\n        $this-&gt;count += $c-&gt;count;\n    }\n<\/code><\/pre>\n<p>Blindingly simple, really, but something I often forget about.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the quick PHP tip of the day: class methods can access the protected (of any shared ancestors) and private (of the same type) members of <i>any<\/i> instance, not only their own instance. That may sound confusing, but it&#8217;s really not so much.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,20,23,32],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-development","category-php","category-programming","category-tips"],"_links":{"self":[{"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":0,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}