Writing Friendly Libraries @seejohnrun
$CI =& get_instance();
 
reactor
<ul><li><?php </li></ul><ul><li>$this -> load -> model ( 'user' ); </li></ul><ul><li>// get the user and their fancy comme...
 
getsparks.org
getsparks.org 76 sparks > 16,000 installs
getsparks.org katzgrau/getsparks.org seejohnrun/sparks
getsparks.org great for internal distribution as well
libraries!!!
rails huge library community centering on rubygems
rails 40 direct 100 total
is it bad?
nah...
let’s share!
the plan <ul><li>things a good library needs </li></ul><ul><li>some strategies for writing more useful libraries </li></ul...
 
test ed
test able
reduce coupling
<ul><li><?php </li></ul><ul><li>// bootstrap.php </li></ul><ul><li>require_once ( 'dependency.php' ); </li></ul><ul><li>De...
nice_http http library easily mock web requests
<ul><li><?php </li></ul><ul><li>NiceHTTP :: register ( function ( $request ) { </li></ul><ul><li>if  ( $request -> isGet (...
 
versioned
0.6.3
0.6. 3 patch version backwards-compatible bug fixes
0. 6 .3 minor version backwards-compatible public functionality
0 .6.3 major version backwards-incompatible public functionality
1.0.0 <ul><li>should mark a milestone </li></ul><ul><li>is a scary place </li></ul>
 
documented
externally PHPDoc
externally visibility
internally PHPDoc
internally comments
<ul><li><?php </li></ul><ul><li>// get the name </li></ul><ul><li>$name   =   'john' ; </li></ul><ul><li>// reverse the na...
 
adaptable
OO
<?php $cache -> set ( 'name' ,  'john' ); $cache -> get ( 'name' );
 
namespaced
 
tip:  use your library  before  you write it
tip:  have someone else read your fake code
tip:  don’t be lazy.  skip out on the shady code
tip:  confusing code doesn’t make you look cool
tip:  pull requests are free code lessons
 
 
no more long library files
use config <?php $this -> load -> library ( 'fun' ,  array ( 'some'   =>   'option' )); <?php class   Fun  { public   func...
use config <?php $this -> load -> library ( 'fun' ,  array ( 'some'   =>   'option' )); <?php class   Fun  { public   func...
use config <?php $CI  = &get_instance(); $CI -> config -> item ( 'fun' );
use lang <?php $CI  = &get_instance(); $CI -> lang -> line ( 'key' );
don’t artificially restrict yourself config/ helpers/ language/ libraries/ models/ views!!!
tip:  stop coding libraries for php4
 
 
tip:  don’t be a downer
tip:  go make and share something amazing
@seejohnrun @getsparks @getsparks
Upcoming SlideShare
Loading in …5
×

Writing Friendly libraries for CodeIgniter

1,980 views
1,883 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,980
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Writing Friendly libraries for CodeIgniter

  1. 1. Writing Friendly Libraries @seejohnrun
  2. 2. $CI =& get_instance();
  3. 4. reactor
  4. 5. <ul><li><?php </li></ul><ul><li>$this -> load -> model ( 'user' ); </li></ul><ul><li>// get the user and their fancy comments </li></ul><ul><li>$user = $this -> user -> get_by_name ( ' john ' ); </li></ul><ul><li>$user -> comments (); </li></ul>http://seejohncode.com/ 2011/02/12/codeigniter- model-instances
  5. 7. getsparks.org
  6. 8. getsparks.org 76 sparks > 16,000 installs
  7. 9. getsparks.org katzgrau/getsparks.org seejohnrun/sparks
  8. 10. getsparks.org great for internal distribution as well
  9. 11. libraries!!!
  10. 12. rails huge library community centering on rubygems
  11. 13. rails 40 direct 100 total
  12. 14. is it bad?
  13. 15. nah...
  14. 16. let’s share!
  15. 17. the plan <ul><li>things a good library needs </li></ul><ul><li>some strategies for writing more useful libraries </li></ul><ul><li>some CI-specific tips </li></ul>
  16. 19. test ed
  17. 20. test able
  18. 21. reduce coupling
  19. 22. <ul><li><?php </li></ul><ul><li>// bootstrap.php </li></ul><ul><li>require_once ( 'dependency.php' ); </li></ul><ul><li>Dependency :: config(); </li></ul><ul><li>// utility.php </li></ul><ul><li>Dependency :: broken(); </li></ul><ul><li>// dependency.php </li></ul><ul><li>class Dependency { </li></ul><ul><li>static function broken () { </li></ul><ul><li>// :( </li></ul><ul><li>} </li></ul><ul><li>static function config () { } </li></ul><ul><li>} </li></ul>
  20. 23. nice_http http library easily mock web requests
  21. 24. <ul><li><?php </li></ul><ul><li>NiceHTTP :: register ( function ( $request ) { </li></ul><ul><li>if ( $request -> isGet () && $request -> hasPath ( '/' )) { </li></ul><ul><li>return NiceHTTPBasicResponse( 200 , 'hi' ); </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul><ul><li>NiceHTTP :: disallowExternalConnections (); </li></ul>
  22. 26. versioned
  23. 27. 0.6.3
  24. 28. 0.6. 3 patch version backwards-compatible bug fixes
  25. 29. 0. 6 .3 minor version backwards-compatible public functionality
  26. 30. 0 .6.3 major version backwards-incompatible public functionality
  27. 31. 1.0.0 <ul><li>should mark a milestone </li></ul><ul><li>is a scary place </li></ul>
  28. 33. documented
  29. 34. externally PHPDoc
  30. 35. externally visibility
  31. 36. internally PHPDoc
  32. 37. internally comments
  33. 38. <ul><li><?php </li></ul><ul><li>// get the name </li></ul><ul><li>$name = 'john' ; </li></ul><ul><li>// reverse the name </li></ul><ul><li>$rname = strrev ( $name ); </li></ul><ul><li>// capitalize the letters of the name </li></ul><ul><li>$name = strtoupper ( $name ); </li></ul><ul><li>// and the rname </li></ul><ul><li>$rname = strtoupper ( $rname ); </li></ul><ul><li>// and then compute a levenshtein distance </li></ul><ul><li>levenshtein ( $name , $rname ); </li></ul>
  34. 40. adaptable
  35. 41. OO
  36. 42. <?php $cache -> set ( 'name' , 'john' ); $cache -> get ( 'name' );
  37. 44. namespaced
  38. 46. tip: use your library before you write it
  39. 47. tip: have someone else read your fake code
  40. 48. tip: don’t be lazy. skip out on the shady code
  41. 49. tip: confusing code doesn’t make you look cool
  42. 50. tip: pull requests are free code lessons
  43. 53. no more long library files
  44. 54. use config <?php $this -> load -> library ( 'fun' , array ( 'some' => 'option' )); <?php class Fun { public function __construct ( $params = array ()) { $this -> option = array_key_exists ( $params , 'some' ) ? $params [ 'some' ] : 'default' ; } }
  45. 55. use config <?php $this -> load -> library ( 'fun' , array ( 'some' => 'option' )); <?php class Fun { public function __construct ( $params = array ()) { $this -> option = isset ( $params[ 'some' ] ) ? $params [ 'some' ] : 'default' ; } }
  46. 56. use config <?php $CI = &get_instance(); $CI -> config -> item ( 'fun' );
  47. 57. use lang <?php $CI = &get_instance(); $CI -> lang -> line ( 'key' );
  48. 58. don’t artificially restrict yourself config/ helpers/ language/ libraries/ models/ views!!!
  49. 59. tip: stop coding libraries for php4
  50. 62. tip: don’t be a downer
  51. 63. tip: go make and share something amazing
  52. 64. @seejohnrun @getsparks @getsparks

×