SlideShare a Scribd company logo
API Design in PHP
          David Sklar
  Software Architect, Ning Inc.
      david@ninginc.com

   DC PHP Conference 2007
Ning Platform
Ning Platform
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 118,000+ networks on the
  platform
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
API: XN_Content
<?php
$dinner = XN_Content::create('Meal');
$dinner->title = 'Salt Baked Combo';
$dinner->my->protein = 'seafood';
$dinner->my->ingredients = 
               array('shrimp','scallops','squid');
$dinner->save();
?>
PHP ➠ REST
POST /xn/atom/1.0/content
Content-Type: text/xml;charset=UTF-8

<entry xmlns=quot;http://www.w3.org/2005/Atomquot;
       xmlns:xn=quot;http://www.ning.com/atom/1.0quot;
       xmlns:my=quot;http://afternoonsnack.ning.com/xn/atom/1.0quot;>
 <xn:type>Meal</xn:type>
 <title type=quot;textquot;>Salt Baked Combo</title>
 <my:protein type='string'>seafood</my:protein>
 <my:ingredients type='string'>
   <xn:value>shrimp</xn:value><xn:value>scallops</xn:value>
   <xn:value>squid</xn:value>
 </my:ingredients>
</entry>
HTTP/1.1 200 OK
                      PHP ➠ REST
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns=quot;http://www.w3.org/2005/Atomquot;
      xmlns:xn=quot;http://www.ning.com/atom/1.0quot;>
 <xn:size>1</xn:size>
 <updated>2007-08-28T22:11:47.420Z</updated>
 <entry xmlns:my=quot;http://afternoonsnack.ning.com/xn/atom/1.0quot;>
  <id>http://afternoonsnack.ning.com/502068:Meal:122</id>
  <xn:type>Meal</xn:type>
  <xn:id>502068:Meal:122</xn:id>
  <title type=quot;textquot;>Salt Baked Combo</title>
  <published>2007-08-28T22:11:47.414Z</published>
  <updated>2007-08-28T22:11:47.414Z</updated>
  <link rel=quot;alternatequot;
        href=quot;http://afternoonsnack.ning.com/xn/detail/502068:Meal:122quot; />
  <my:protein type=quot;stringquot;>seafood</my:protein>
  ...
 </entry>
</feed>
Design Priorities

• Promote predictability, modularity, stability
• Choose human performance over computer
  performance
• Make efficiency easy, make inefficiency hard/
  impossible
At the start...

• Write code before you write the API
• Use cases, Use cases, Use cases
• Names matter (but don’t discuss them
  forever)
Use the API before it
            exists
Sketch out what you want to do....
Use Cases First!


• What does the API need to do?
• (Not “what could it do?”)
Need-driven
        Development

• Adding is easy. Removing is hard.
• You have lots of freedom with arguments
• Accessors provide insulation
Arguments
Long parameter lists are toxic:
<?php

function save($data, $flavor = null, $scope = null,
              $commit = null, $cascade = null,
              $permissions = null) {
    if (is_null($flavor))      { $flavor = 'quick'; }
    if (is_null($scope))       { $scope = 'global'; }
    if (is_null($commit))      { $commit = true; }
    if (is_null($cascade))     { $cascade = false; }
    if (is_null($permissions)) { $permissions = 0755; }
    // ...
}
What does this do?
<?php

save($data, null, null, true, false);

?>
Bread Stuffing
Bread Stuffing
How about this?
<?php

save($data, array('scope' => 'local'));

?>
Ahh, much better:
<?php

function save($data, $paramsOrFlavor = null,
               $scope = null, $commit = null,
              $cascade = null, $permissions = null){
    if (is_array($paramsOrFlavor)) {
        // ...
    }
    else {
        // ...
    }
}
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':
      // ...
}
Static ‘n’ Dynamic Analysis



• find + grep
• tokenizer
• hooks + logging
find + grep
find . -name *.php -exec grep -H '::load('

   • easy
   • fast
   • mostly correct: watch out for dynamic
     variable names, text collision, etc.
tokenizer


• php-specific knowledge, but...
• can be slower
• need to write custom rules and parsing
$tokens = array(T_INCLUDE => 0, T_INCLUDE_ONCE => 0,
                T_REQUIRE => 0, T_REQUIRE_ONCE => 0);

foreach (new PhpFilterIterator(new RecursiveIteratorIterator(
         new RecursiveDirectoryIterator($root))) as $f) {
  $muncher = new Tokenmunch(file_get_contents($f));
  foreach ($muncher as $token) {
    if (array_key_exists($token[0], $tokens)) {
      $tokens[$token[0]]++;
      $startOfLine = $muncher->scanBackFor(T_WHITESPACE,quot;/n/quot;);
      $startOfBlock = $muncher->scanBackFor(T_OPEN_TAG);
      $previousComment = $muncher->scanBackFor(T_COMMENT,quot;/n$/quot;);
      $startPosition = max($startOfLine, $startOfBlock, $previousComment) +1;
      $endOfLine = $muncher->scanForwardFor(T_STRING, '/^;$/');
      $slice =  $muncher->sliceAsString($startPosition,
                                       $endOfLine - $startPosition+1);
      print trim($slice) . quot;nquot;;
    }
  }
}
Hooks + Logging


• need to instrument the API beforehand
• watch out for performance overhead
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);
}
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 and
selective runtime debugging instrumentation.
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
           );
Names Matter


• Namespacing / collisions
• Versioning
Namespacing

• At Ning, “XN” means “hands off”
 • class names
 • property names
 • xml namespace prefixes
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/...
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() {
}
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() {
}
Too Much Sugar
// quot;Systemquot; Attribute
$content->title = 'Duck with Pea Shoots';

// quot;Developerquot; Attribute
$content->my->meat = true;
Non-literal Names = HFCS

$attrs = array('title','my->meat');

foreach ($attrs as $attr) {
    print quot;$attr is quot; . $content->$attr;
}




            ☹
Alternatives

OK $content->title    and $content->my_flavor;

OK $content->xn_title and $content->flavor;

NO $content['title']  and $content->flavor;
Testing
      &
Code Coverage
The extent of your
        test suite
          is the
strength of your contract
     with your users.
Tools are secondary.

Discipline is primary.
Tools

• SimpleTest
 •   http://www.lastcraft.com/simple_test.php

• PHPUnit
 •   http://phpunit.de/




                                         ginger monkey - popofatticus@flickr - CC attrib 2.0
                                                         gorilla: tancread@flickr - CC by-nc
To Keep in Mind...
• Lean towards use cases rather than
  unconstrained possibilities
• Naming, versioning, and documentation are
  not afterthoughts.
• Test suite code coverage is all you have to
  guarantee backwards-compatibility
• Sugar, yes; HFCS, no.
Resources
•   Joshua Bloch: quot;How to Design a Good API and
    Why It Mattersquot;

    •   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/
Come work at                         !

• Write the code that powers 118,000+ social
  networks
• Write the next version of our PHP API
• Work in Palo Alto (or not)
• http://jobs.ning.com - david@ninginc.com

More Related Content

What's hot

Overview of PHP and MYSQL
Overview of PHP and MYSQLOverview of PHP and MYSQL
Overview of PHP and MYSQL
Deblina Chowdhury
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
Bradley Holt
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
Dave Cross
 
PHP 7 Crash Course - php[world] 2015
PHP 7 Crash Course - php[world] 2015PHP 7 Crash Course - php[world] 2015
PHP 7 Crash Course - php[world] 2015
Colin O'Dell
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
Mark Baker
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
Shweta A
 
Last train to php 7
Last train to php 7Last train to php 7
Last train to php 7
Damien Seguy
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
Php mysql
Php mysqlPhp mysql
Php mysql
Abu Bakar
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQLJussi Pohjolainen
 
Php mysql
Php mysqlPhp mysql
Rest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemyRest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemy
Alessandro Cucci
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
Php 7 hhvm and co
Php 7 hhvm and coPhp 7 hhvm and co
Php 7 hhvm and co
Pierre Joye
 

What's hot (18)

Overview of PHP and MYSQL
Overview of PHP and MYSQLOverview of PHP and MYSQL
Overview of PHP and MYSQL
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
 
PHP 7 Crash Course - php[world] 2015
PHP 7 Crash Course - php[world] 2015PHP 7 Crash Course - php[world] 2015
PHP 7 Crash Course - php[world] 2015
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
Php security3895
Php security3895Php security3895
Php security3895
 
Last train to php 7
Last train to php 7Last train to php 7
Last train to php 7
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Rest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemyRest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemy
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Php 7 hhvm and co
Php 7 hhvm and coPhp 7 hhvm and co
Php 7 hhvm and co
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 

Viewers also liked

teamitt
teamittteamitt
Teamitt
TeamittTeamitt
Bo Dvs.Sab
Bo Dvs.SabBo Dvs.Sab
Harmonypark Style Slam Garage
Harmonypark Style Slam GarageHarmonypark Style Slam Garage
Harmonypark Style Slam Garage
sumithra jonnalagadda
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
In a Rocket
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media Plan
Post Planner
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
Kirsty Hulse
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
ux singapore
 

Viewers also liked (8)

teamitt
teamittteamitt
teamitt
 
Teamitt
TeamittTeamitt
Teamitt
 
Bo Dvs.Sab
Bo Dvs.SabBo Dvs.Sab
Bo Dvs.Sab
 
Harmonypark Style Slam Garage
Harmonypark Style Slam GarageHarmonypark Style Slam Garage
Harmonypark Style Slam Garage
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media Plan
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
 

Similar to Api Design

PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
rICh morrow
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
Rowan Merewood
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
Elizabeth Smith
 
Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009
Bastian Feder
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
ZendCon
 
Becoming A Php Ninja
Becoming A Php NinjaBecoming A Php Ninja
Becoming A Php Ninja
Mohammad Emran Hasan
 
Lean Php Presentation
Lean Php PresentationLean Php Presentation
Lean Php Presentation
Alan Pinstein
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.jssouridatta
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
Perl Careers
 
php 1
php 1php 1
php 1
tumetr1
 
Streamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web FrameworksStreamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web Frameworks
guestf7bc30
 
Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-php
Richard McIntyre
 
Basics PHP
Basics PHPBasics PHP
Revoke-Obfuscation
Revoke-ObfuscationRevoke-Obfuscation
Revoke-Obfuscation
Daniel Bohannon
 
Lecture8
Lecture8Lecture8
Lecture8
Majid Taghiloo
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
Php Development With Eclipde PDT
Php Development With Eclipde PDTPhp Development With Eclipde PDT
Php Development With Eclipde PDT
Bastian Feder
 
Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009
PHPBelgium
 

Similar to Api Design (20)

PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
 
Becoming A Php Ninja
Becoming A Php NinjaBecoming A Php Ninja
Becoming A Php Ninja
 
Lean Php Presentation
Lean Php PresentationLean Php Presentation
Lean Php Presentation
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
 
php 1
php 1php 1
php 1
 
Streamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web FrameworksStreamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web Frameworks
 
Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-php
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Revoke-Obfuscation
Revoke-ObfuscationRevoke-Obfuscation
Revoke-Obfuscation
 
Lecture8
Lecture8Lecture8
Lecture8
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php Development With Eclipde PDT
Php Development With Eclipde PDTPhp Development With Eclipde PDT
Php Development With Eclipde PDT
 
Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009Prepare for PHP Test Fest 2009
Prepare for PHP Test Fest 2009
 

Recently uploaded

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 

Recently uploaded (20)

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 

Api Design

  • 1. API Design in PHP David Sklar Software Architect, Ning Inc. david@ninginc.com DC PHP 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 118,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
  • 7. PHP ➠ REST POST /xn/atom/1.0/content Content-Type: text/xml;charset=UTF-8 <entry xmlns=quot;http://www.w3.org/2005/Atomquot; xmlns:xn=quot;http://www.ning.com/atom/1.0quot; xmlns:my=quot;http://afternoonsnack.ning.com/xn/atom/1.0quot;> <xn:type>Meal</xn:type> <title type=quot;textquot;>Salt Baked Combo</title> <my:protein type='string'>seafood</my:protein> <my:ingredients type='string'> <xn:value>shrimp</xn:value><xn:value>scallops</xn:value> <xn:value>squid</xn:value> </my:ingredients> </entry>
  • 8. HTTP/1.1 200 OK PHP ➠ REST <?xml version='1.0' encoding='UTF-8'?> <feed xmlns=quot;http://www.w3.org/2005/Atomquot; xmlns:xn=quot;http://www.ning.com/atom/1.0quot;> <xn:size>1</xn:size> <updated>2007-08-28T22:11:47.420Z</updated> <entry xmlns:my=quot;http://afternoonsnack.ning.com/xn/atom/1.0quot;> <id>http://afternoonsnack.ning.com/502068:Meal:122</id> <xn:type>Meal</xn:type> <xn:id>502068:Meal:122</xn:id> <title type=quot;textquot;>Salt Baked Combo</title> <published>2007-08-28T22:11:47.414Z</published> <updated>2007-08-28T22:11:47.414Z</updated> <link rel=quot;alternatequot; href=quot;http://afternoonsnack.ning.com/xn/detail/502068:Meal:122quot; /> <my:protein type=quot;stringquot;>seafood</my:protein> ... </entry> </feed>
  • 9. Design Priorities • Promote predictability, modularity, stability • Choose human performance over computer performance • Make efficiency easy, make inefficiency hard/ impossible
  • 10. At the start... • Write code before you write the API • Use cases, Use cases, Use cases • Names matter (but don’t discuss them forever)
  • 11. Use the API before it exists Sketch out what you want to do....
  • 12. Use Cases First! • What does the API need to do? • (Not “what could it do?”)
  • 13. Need-driven Development • Adding is easy. Removing is hard. • You have lots of freedom with arguments • Accessors provide insulation
  • 14. Arguments Long parameter lists are toxic: <?php function save($data, $flavor = null, $scope = null, $commit = null, $cascade = null, $permissions = null) {     if (is_null($flavor))      { $flavor = 'quick'; }     if (is_null($scope))       { $scope = 'global'; }     if (is_null($commit))      { $commit = true; }     if (is_null($cascade))     { $cascade = false; }     if (is_null($permissions)) { $permissions = 0755; }     // ... }
  • 15. What does this do? <?php save($data, null, null, true, false); ?>
  • 19. Ahh, much better: <?php function save($data, $paramsOrFlavor = null, $scope = null, $commit = null,               $cascade = null, $permissions = null){     if (is_array($paramsOrFlavor)) {         // ...     }     else {         // ...     } }
  • 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': // ... }
  • 21. Static ‘n’ Dynamic Analysis • find + grep • tokenizer • hooks + logging
  • 22. find + grep find . -name *.php -exec grep -H '::load(' • easy • fast • mostly correct: watch out for dynamic variable names, text collision, etc.
  • 23. tokenizer • php-specific knowledge, but... • can be slower • need to write custom rules and parsing
  • 24. $tokens = array(T_INCLUDE => 0, T_INCLUDE_ONCE => 0,                 T_REQUIRE => 0, T_REQUIRE_ONCE => 0); foreach (new PhpFilterIterator(new RecursiveIteratorIterator( new RecursiveDirectoryIterator($root))) as $f) { $muncher = new Tokenmunch(file_get_contents($f));   foreach ($muncher as $token) {     if (array_key_exists($token[0], $tokens)) {       $tokens[$token[0]]++;       $startOfLine = $muncher->scanBackFor(T_WHITESPACE,quot;/n/quot;);       $startOfBlock = $muncher->scanBackFor(T_OPEN_TAG);       $previousComment = $muncher->scanBackFor(T_COMMENT,quot;/n$/quot;);       $startPosition = max($startOfLine, $startOfBlock, $previousComment) +1;       $endOfLine = $muncher->scanForwardFor(T_STRING, '/^;$/');       $slice =  $muncher->sliceAsString($startPosition, $endOfLine - $startPosition+1);       print trim($slice) . quot;nquot;;     }   } }
  • 25.
  • 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 and selective runtime debugging instrumentation.
  • 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 );
  • 30. Names Matter • Namespacing / collisions • Versioning
  • 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() { }
  • 36. Non-literal Names = HFCS $attrs = array('title','my->meat'); foreach ($attrs as $attr) {     print quot;$attr is quot; . $content->$attr; } ☹
  • 37. Alternatives OK $content->title  and $content->my_flavor; OK $content->xn_title and $content->flavor; NO $content['title']  and $content->flavor;
  • 38. Testing & Code Coverage
  • 39. The extent of your test suite is the strength of your contract with your users.
  • 41. Tools • SimpleTest • http://www.lastcraft.com/simple_test.php • PHPUnit • http://phpunit.de/ ginger monkey - popofatticus@flickr - CC attrib 2.0 gorilla: tancread@flickr - CC by-nc
  • 42. To Keep in Mind... • Lean towards use cases rather than unconstrained possibilities • Naming, versioning, and documentation are not afterthoughts. • Test suite code coverage is all you have to guarantee backwards-compatibility • Sugar, yes; HFCS, no.
  • 43. Resources • Joshua Bloch: quot;How to Design a Good API and Why It Mattersquot; • 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/
  • 44. Come work at ! • Write the code that powers 118,000+ social networks • Write the next version of our PHP API • Work in Palo Alto (or not) • http://jobs.ning.com - david@ninginc.com