writing friendly libraries for codeigniter
TRANSCRIPT
Writing Friendly Libraries@seejohnrun
$CI =& get_instance();
reactor
<?php<?php$this$this->->loadload->->modelmodel(('user''user'););// get the user and their fancy comments// get the user and their fancy comments$user$user == $this$this->->useruser->->get_by_nameget_by_name((''johnjohn''););$user$user->->commentscomments();();
http://seejohncode.com/2011/02/12/codeigniter-model-instances
getsparks.org
getsparks.org76 sparks
> 16,000 installs
getsparks.orgkatzgrau/getsparks.org
seejohnrun/sparks
getsparks.orggreat for internal distribution
as well
libraries!!!
railshuge library community centering on rubygems
rails40 direct100 total
is it bad?
nah...
let’s share!
the plan•things a good library needs•some strategies for writing
more useful libraries•some CI-specific tips
tested
testable
reduce coupling
<?php<?php// bootstrap.php// bootstrap.phprequire_oncerequire_once(('dependency.php''dependency.php'););DependencyDependency::::config();config();
// utility.php// utility.phpDependencyDependency::::broken();broken();
// dependency.php// dependency.phpclassclass DependencyDependency { { static static functionfunction brokenbroken() {() { // :(// :( }} static static functionfunction configconfig() { }() { }}}
nice_httphttp library
easily mock web requests
<?php<?phpNiceHTTPNiceHTTP::::registerregister((functionfunction(($request$request) {) { ifif ( ($request$request->->isGetisGet() () &&&& $request$request->->hasPathhasPath(('/''/')) {)) { returnreturn NiceHTTP\BasicResponse( NiceHTTP\BasicResponse(200200, , 'hi''hi');); }}});});
NiceHTTPNiceHTTP::::disallowExternalConnectionsdisallowExternalConnections();();
versioned
0.6.3
0.6.3patch version
backwards-compatible bug fixes
0.6.3minor version
backwards-compatible public functionality
0.6.3major version
backwards-incompatible public functionality
1.0.0•should mark a milestone
•is a scary place
documented
externallyPHPDoc
externallyvisibility
internallyPHPDoc
internallycomments
<?php<?php// get the name// get the name$name$name == 'john''john';;// reverse the name// reverse the name$rname$rname == strrevstrrev(($name$name););// capitalize the letters of the name// capitalize the letters of the name$name$name == strtoupperstrtoupper(($name$name););// and the rname// and the rname$rname$rname == strtoupperstrtoupper(($rname$rname););// and then compute a levenshtein distance// and then compute a levenshtein distancelevenshteinlevenshtein(($name$name, , $rname$rname););
adaptable
OO
<?php<?php$cache$cache->->setset(('name''name', , 'john''john'););$cache$cache->->getget(('name''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<?php$this$this->->loadload->->librarylibrary(('fun''fun', , arrayarray(('some''some' =>=> 'option''option'));));
<?php<?phpclassclass FunFun { { publicpublic functionfunction __construct__construct(($params$params == arrayarray()) {()) { $this$this->->optionoption == array_key_existsarray_key_exists(($params$params, , 'some''some') ) ?? $params$params[['some''some'] ] :: 'default''default';; }}}}
use config<?php<?php$this$this->->loadload->->librarylibrary(('fun''fun', , arrayarray(('some''some' =>=> 'option''option'));));
<?php<?phpclassclass FunFun { { publicpublic functionfunction __construct__construct(($params$params == arrayarray()) {()) { $this$this->->optionoption == issetisset(($params[$params['some''some']]) ) ?? $params$params[['some''some'] ] :: 'default''default';; }}}}
use config<?php<?php$CI$CI = &get_instance(); = &get_instance();$CI$CI->->configconfig->->itemitem(('fun''fun'););
use lang
<?php<?php$CI$CI = &get_instance(); = &get_instance();$CI$CI->->langlang->->lineline(('key''key'););
don’t artificially restrict yourself
config/config/helpers/helpers/language/language/libraries/libraries/models/models/views!!!views!!!
tip: stop coding libraries for php4
tip: don’t be a downer
tip: go make and share something
amazing
@seejohnrun@getsparks@getsparks