Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP For Dummies

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

PHP For Dummies

Avatar for Chad Cunningham

Chad Cunningham

July 11, 2014

Other Decks in Technology

Transcript

  1. Modern php development has come a long way • But

    we’re not using modern PHP practices… • And our legacy code makes babies weep • But we can still make it suck less!
  2. Why do we care? • CMM 2015 2016 2017 will

    fix everything anyway • The code is already terrible, it’s not like a little more bad code will hurt anything • PHP isn’t even a good language to begin with
  3. We care because… • No one likes writing bad code

    • Refactoring terrible code is rewarding and a valuable skill • PHP handles most of our traffic and has the biggest impact towards users • PHP makes us money
  4. What’s wrong with this code? ! (Hint: 7 things) function

    controllerAction() { $user = User::getUser(); $request = new Request($_POST[‘request_id’]); $form = Form::getForm($_POST[‘form_id’]); ! if ( isset( $form ) ) { $request->form_id = $form; $request->save(); $request->touch($touch_type); } ! $this->redirect(“request/list”); }
  5. Things to look for • Don’t trust user input, validate

    all GET/POST data • Don’t assume a user who isn’t logged in will not hit your controller action • Don’t assume a user has access to a request • Check function return types and values • If you refactor code, check to make sure all variables you’re using are defined
  6. You should always: • Access request as anonymous user and

    fix errors • Access requests without POST/GET data and fix any errors • Monitor error logs / errbit and fix any errors or warnings • If you see some bad code, take a minute to fix it, even if you didn’t write it • If you’re not comfortable with PHP, ask someone who is if you have any questions
  7. Code Smells • Static functions, especially classes with nothing but

    static functions • Deeply nested methods • Long methods • Long sections of if / else blocks (abstract or fail early) • Complex boolean expressions • Duplicated code • Long function argument list • Functions that return multiple types ( e.g. array or false )
  8. Coming soon with PHP 5.4 • Closures / Anonymous Functions

    • Short Arrays ( $array = [‘one’, ‘two’] ) • Traits (like ruby mixin modules) • Autloading • Composer (like bundler) • Symfony Components • PHPUnit • CMM Console!
  9. Key Points • Be mindful of the code you write

    and look out for common errors / mistakes • Be extremely careful with any code involving request touches • Leave the place a little cleaner than you found it • Don’t be afraid to pull code out into smaller single purpose objects (which you can then test) • When in doubt, ask for help