{"id":6,"date":"2008-02-15T13:54:15","date_gmt":"2008-02-15T18:54:15","guid":{"rendered":"http:\/\/www.timrosenblatt.com\/blog\/2008\/02\/15\/how-to-make-a-single-function-call-handle-one-or-many\/"},"modified":"2008-02-15T14:04:30","modified_gmt":"2008-02-15T19:04:30","slug":"how-to-make-a-single-function-call-handle-one-or-many","status":"publish","type":"post","link":"http:\/\/www.timrosenblatt.com\/blog\/2008\/02\/15\/how-to-make-a-single-function-call-handle-one-or-many\/","title":{"rendered":"How to make a single function call handle one or many"},"content":{"rendered":"<p>I was browsing through <a href=\"http:\/\/www.ning.com\/\">Ning&#8217;s<\/a> codebase the other day. I work directly with the code since I&#8217;ve <a href=\"http:\/\/www.purpleflorida.com\/\">customized a Ning network<\/a> for one of Cloudspace&#8217;s clients in some subtle ways, just because he wanted it that way.<\/p>\n<p>I really like the work they&#8217;ve done. Their codebase clearly has a lot of best practices.<\/p>\n<p>I found one neat trick that was used to make a function a bit more versatile, in that it can easily be passed a single object, or an array of objects, with no sweat. If you&#8217;re interested, you can find an example of this in \/widgets\/activity\/models\/ActivityLogItem.php, in beforeDelete()<\/p>\n<p>This isn&#8217;t their code, but here&#8217;s an example of the trick.<\/p>\n<pre>\r\nfunction eatPie($pie_id) {\r\n    foreach( (is_array($pie_id) ? $pie_id : array($pie_id)) as $current_id) {\r\n        \/\/ codez\r\n    }\r\n}<\/pre>\n<p>Really straightforward. If an array is passed in, each item in the array gets operated on. If a single item is passed in, it gets turned into an array, then each item in the array gets operated on. I also like how the tertiary statement gets used inline with the foreach. Of course, if you want a bit more code clarity, you can do it like this:<\/p>\n<pre>\r\nfunction eatPie($pie_id) {\r\n    $pie_id = is_array($pie_id) ? $pie_id : array($pie_id);\r\n    foreach($pie_id  as $current_id) {\r\n        \/\/ codez\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was browsing through Ning&#8217;s codebase the other day. I work directly with the code since I&#8217;ve customized a Ning network for one of Cloudspace&#8217;s clients in some subtle ways, just because he wanted it that way. I really like the work they&#8217;ve done. Their codebase clearly has a lot of best practices. I found &hellip; <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/02\/15\/how-to-make-a-single-function-call-handle-one-or-many\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to make a single function call handle one or many&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,3],"_links":{"self":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/6"}],"collection":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":0,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}