Understanding‎ > ‎APIs‎ > ‎


Notes on what appears to be common (and best) practices when writing a PHP library.

What's the best way to write a PHP library? The conventional wisdom seems to be:
  • create a class, not just functions
  • constructor takes application keys as the parameters
  • class method names should closely match the names of endpoint URLs
  • use PHP arrays or objects as the methods' return values then unserialize (at least that's what I do) when possible
    • other than the LongURL web service's API that I wrote a library for, I haven't seen an API that returns PHP in the same way
    • arrays or objects? Closely match what the API gives you? For example, if it's JSON or XML, return objects? 
    • if it's XML, SimpleXML objects are more difficult to address than StdClas() objects. What to do then?
    • should we prefer JSON from the API itself? JSONP? What if the JSON returns a callback which we can't turn off? One Stack Overflow post recommends str_replace() if that's the case, but that's messy!