{"id":5383,"date":"2014-03-30T21:17:49","date_gmt":"2014-03-30T21:17:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/require-1-of-2-arguments-in-a-method-function-one-of-two-arguments-optional-collection-of-common-programming-errors\/"},"modified":"2014-03-30T21:17:49","modified_gmt":"2014-03-30T21:17:49","slug":"require-1-of-2-arguments-in-a-method-function-one-of-two-arguments-optional-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/require-1-of-2-arguments-in-a-method-function-one-of-two-arguments-optional-collection-of-common-programming-errors\/","title":{"rendered":"Require 1 of 2 arguments in a method \/ function (one of two arguments optional)-Collection of common programming errors"},"content":{"rendered":"<p>If it can only use one or the other, then you have state. If you have state, you need polymorphism, or in this very small use-case, two different methods.<\/p>\n<pre><code>public function getById($id) {}\npublic function getByUrl($url) {}\n<\/code><\/pre>\n<p>That&#8217;s the end of it. That&#8217;s the best you can do for yourself.<\/p>\n<p><strong>if and only if<\/strong> you don&#8217;t know in advance which is being passed in, (like the case of user input), you can make a <em>third<\/em> method, to parse and validate the input string, determine which type it is, and call the appropriate method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If it can only use one or the other, then you have state. If you have state, you need polymorphism, or in this very small use-case, two different methods. public function getById($id) {} public function getByUrl($url) {} That&#8217;s the end of it. That&#8217;s the best you can do for yourself. if and only if you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5383","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5383"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5383\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}