The document discusses API design in PHP for the Ning platform. It covers how the PHP API provides an interface to the Ning REST APIs, which are used for content storage, user profiles, tagging, search, and more. Examples are given of using the PHP XN_Content class to create and save content to the REST API. The document emphasizes designing APIs that promote predictability, stability, and human performance over computer performance.
1 of 46
More Related Content
Zendcon 2007 Api Design
1. API Design in PHP
David Sklar
Software Architect, Ning Inc.
david@ninginc.com
Zend Conference 2007
4. Ning
• PHP API provides interface to our platform
REST APIs
• Live since August 2005 (with 5.0.4)
• Recent upgrade to 5.2.3
• In use in all 108,000+ networks on the
platform
5. Ning
• Migration from XMLRPC to REST in 2005/6
• APIs used for content storage, user profile
management, tagging, search, video
transcoding, messaging, ...
• PHP (using APIs) runs in a hosted
environment
20. Fun with __get() and __set()
public function __get($name) {
switch ($name) {
case self::screenName:
return $this->_screenName;
case self::fullName:
return $this->_fullName;
case self::uploadEmailAddress:
$this->_lazyLoad('uploadEmailAddress');
return $this->_uploadEmailAddress;
case 'description':
// ...
}
26. Hooks + Logging
• need to instrument the API beforehand
• watch out for performance overhead
27. API for the API: XN_Event
class XN_Event {
/**
* Fire an event with optional arguments
*
* @param string $event
* @param array $args optional arguments to pass to listeners
*/
public static function fire($event, $args = null);
/**
* Listen for an event
*
* @param string $event
* @param callback $callback Function to run when the event is fired
* @param array $args optional arguments to pass to the callback
* @return string
*/
public static function listen($event, $callback, $args = null);
}
28. XN_Event in Use
XN_Content::save() calls:
XN_Event::fire('xn/content/save/before', array($this));
// do the save
XN_Event::fire('xn/content/save/after', array($this));
This has been very useful for cache expiration.
29. Late Static Binding Workaround
Class name registry for static inheritance:
W_Cache::putClass('app','XG_App');
// ... time passes ...
$className = W_Cache::getClass($role);
$retval = call_user_func_array(
array($className, $method),
$args
);
31. Namespacing
• At Ning, “XN” means “hands off”
• class names
• property names
• xml namespace prefixes
32. Versioning
• YourClass and YourClass2...sigh.
• Using include_path and auto_prepend_file
• Version number in REST URLs:
http://app.ning.com/xn/atom/1.0/content/...
33. Docblocks:Yay!
/** It’s easy to generate human(-ish)
* readable documentation from
* docblocks. (@see PHPDocumentor,
* @see doxygen)
*
* And the documentation is close
* to the code.
*/
public function debigulator() {
}
34. Docblocks: Boo!
/** What about examples and tutorials
* and all of the other thing that
* are not method or class
* specific?
*
* Is this documentation up to date
* with the @version of the code?
*/
public function rebigulator() {
}
45. Resources
• Joshua Bloch: "How to Design a Good API and
Why It Matters"
• http://lcsd05.cs.tamu.edu/slides/keynote.pdf
• Zend Framework Documentation
• http://framework.zend.com/manual/manual/
• eZ Components Documentation
• http://ez.no/doc/components/overview/
• These slides: http://www.sklar.com/blog/
46. Come work at !
• Write the code that powers 100,000+ social
networks
• Write the next version of our PHP API
• Work in Palo Alto (or not)
• http://jobs.ning.com - david@ninginc.com