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

PHP For Grown Ups

linden3
April 14, 2015

PHP For Grown Ups

Slides of the talk as given at the PHPAmersfoort Meetup on April 14, 2015

linden3

April 14, 2015
Tweet

Other Decks in Programming

Transcript

  1. “Always code as if the guy who ends up maintaining

    your code will be a violent psychopath who knows where you live” - probably John Woods
  2. “Shy code - modules that don’t reveal anything unnecessary to

    other modules and that don’t rely on other modules’ implementations” - The Pragmatic Programmer
  3. $totalPrice = 0; foreach ($order->getItems() as $item) { $amount =

    $item->getAmount(); $price = $item->getPrice(); $totalPrice += $amount * $price; } $totalPrice += $order->getShippingCosts();
  4. “MATHEMATICS ABSTRACTION IS THE ART OF GIVING THE SAME NAME

    TO DIFFERENT THINGS.” ~ HENRI POINCARÉ’S PROGRAMMER COUSIN
  5. /** * before: */ public function search($searchTerm, $productCategory, $make, $color)

    { switch ($productType) { case ‘car’: // search cars case ‘boat’: // search boat // etc. } }
  6. /** * after: */ public function search(SearchQuery $query) { $worker

    = $this->getWorkerForQuery($query); /** @var SearchResults $results */ $results = $worker->process($query); return $results; }
  7. “Figuring out if two people are married is easy. Figuring

    out if they should be is hard.” - Michael Feathers
  8. LOW LEVEL OF ABSTRACTION $statement = $database->prepare( “INSERT INTO `fish`(‘name’,

    ‘species’, ‘gender’, ‘tankId’) ” . “VALUES(?, ?, ?, ?)” ); $statement->execute(array(‘Nemo’, $speciesId, ‘M’, $tankId));
  9. “THERE ARE ONLY TWO HARD THINGS IN COMPUTER SCIENCE: CACHE

    INVALIDATION AND NAMING THINGS” ~ PHIL KARLTON
  10. NAMES THAT LIE // neither of these is actually a

    locale $this->locale = $locale->locale; // does more than it says public function getItem() { // write to disk }
  11. EXTRACT METHOD // before: if (is_numeric($amount) && $amount > 0)

    // after: if ($this->isValidAmount($amount))
  12. // set the patient’s clinic $patient->setClinic($clinic); // to register a

    patient, set the patient’s clinic $patient->setClinic($clinic);
  13. “I REUSE CODE IF, AND ONLY IF, I NEVER NEED

    TO LOOK AT THE SOURCE CODE” ~ UNCLE BOB
  14. if (! $amount) { // do something } // $amount

    can actually be 0, “0”, “”, null or false for this to be true
  15. if ($amount == 0) { // do something } //

    $amount can now still be 0, “0” or false for this to be true
  16. if ($amount === 0) { // do something } //

    this will only evaluate to true when $amount is actually the number 0
  17. “THE MORE I LEARN, THE MORE I REALIZE HOW MUCH

    I DON’T KNOW” ~ ALBERT EINSTEIN
  18. BOOKS Growing Object-Oriented Software, Guided By Tests The Pragmatic Programmer

    Domain-Driven Design Patterns of Enterprise Application Architecture Clean Code
  19. VIDEOS Models and Service Layers; Hemoglobin and Hobgoblins (https://www.youtube.com/watch?v=3uV3ngl1Z8g) Unbreakable

    Domain Models (https://www.youtube.com/watch?v=ZJ63ltuwMaE) The Framework As An Implementation Detail (https://www.youtube.com/watch?v=0L_9NutiJlc)
  20. “A CLASS SHOULD HAVE ONE, AND ONLY ONE, REASON TO

    CHANGE.” ~ SOLID PRINCIPLE #1 (SINGLE RESPONSIBILITY PRINCIPLE)
  21. “A CLASS SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR

    MODIFICATION” ~ SOLID PRINCIPLE #2 (OPEN-CLOSED PRINCIPLE)
  22. “SUBCLASSES MUST BE SUBSTITUTABLE FOR THEIR BASE CLASSES” ~ SOLID

    PRINCIPLE #3 (LISKOV SUBSTITUTION PRINCIPLE)
  23. ”NEVER FORCE CLASSES TO IMPLEMENT METHODS THAT THEY DO NOT

    USE” ~ SOLID PRINCIPLE #4 (INTERFACE SEGREGATION PRINCIPLE)