Writing Effective PHP

Writing Effective PHP

- Type: Mid-level
- Length: 30min

## If I attend your session, what practical capability will I bring back to my team and be able to implement next week?

This talk is designed to help you make effective use of modern PHP. You will learn about: Preventing bugs using defensive programming, designing and creating objects, when and how to use immutability, write the public API of a class, composition over inheritance and how to write and set up high-quality tests.

## Summary

PHP has evolved massively since its first days, and it's on the road to a full-featured language. However, at the same time, there is a lot of outdated information and libraries with poorly written code.

This talk is about writing PHP Code that is: safe, robust, and maintainable. We are going to cover a set of rules, that if you follow them; they will get you on the habit of writing code well.

Get ready for a 25 min live-coding session, where I go through practical examples. You will learn how to design classes, how to think about your API to prevent breaking changes, and discover concepts around designing and creating objects, when and how to use immutability, and much more.

## Speaker

Salut! My name is Nuno Maduro, and I am a member of the PHP community for a long time now. Last year, I made a total of 2,562 open source contributions. And I have spoken at conferences such as PHP Serbia, Laracon EU, ForumPHP France, or Laravel Live India.

I have a close relationship with the Laravel community, as I have contributed to the core of the framework. I also have created tons of open source packages such as PHP Insights, Laravel Zero, Scout Extended, Collision, Larastan, and more.

I also maintain the open source API clients and integrations at Algolia, where I have to write robust, safe, and maintainable code.

Early this year, I have launched my premium video course: https://nunomaduro.com/writing-effective-php.

8c6da099bca5cd7c16d8064ca6efaca6?s=128

Nuno Maduro

June 17, 2019
Tweet

Transcript

  1. Writing EFFECTIVE PHP by Nuno Maduro

  2. DEFENSIVE PROGRAMMING Assume the worst

  3. EVERY LINE OF CODE IS A LIABILITY Just like driving

  4. PLEASE CONSIDER THIS PRACTICES ROBUST TESTABLE SAFE MAINTAINABLE Prevent every

    possible bug
  5. PRATICES Depends of TEAM & PROJECT

  6. LET'S GET STARTED

  7. Make State IMMUTABLE

  8. HOW WRITE IMMUTABLE CLASSES? ▸ Constructor is the only Injection

    Point ▸ No setters ▸ Private Attributes ▸ Make state private
  9. Always ASSERT

  10. WHEN TO ASSERT INPUT? ▸ Always ▸ Build your own

    helpers to avoid code duplication ▸ Consider the package "beberlei/assert"
  11. public function create(string $name, array $objects): void { Assertion::notEmpty($name); Assertion::allIsInstanceOf($objects,

    \App\User::class); }
  12. PUBLIC API Clear Contract

  13. class Money { /** * I know this sucks! But

    I have deployed * this in a rush last friday ahahah. Good Luck! */ public function __construct($amount); }
  14. /** * Takes the `amount` of the instance attribute `$money`

    to create {...} using * the instance attribute `$dataTime` for the field `created_at`. * * @throws \Domain\Exceptions\SomethingNotAvailable If something is not available. */ public function handle(): void
  15. If you use Semantic Versioning DECLARE A PUBLIC API

  16. HOW WRITE A SOLID CONTRACT? ▸ What is it ▸

    What it does ▸ What it returns ▸ What is the behavior ▸ What is the behavior when the contract gets violated
  17. Make classes FINAL BY DEFAULT

  18. class Db { /* ... */ } class Core extends

    Db { /* ... */ } class User extends Core { /* ... */ } class Admin extends User { /* ... */ } class Bot extends Admin { /* ... */ } class BotThatDoesSpecialThings extends Bot { /* ... */ } class PatchedBot extends BotThatDoesSpecialThings { /* ... */ } class PatchedBotVersion2019 extends PatchedBot { /* ... */ }
  19. class Food {} class Human extends Food {} class Nuno

    extends Human {} $nuno = new Nuno(); $nuno->eat();
  20. COMPOSITION OVER INHERITANCE final class Food {} $food = new

    Food(); $nuno = new Nuno($food) $nuno->eat();
  21. WHEN MARK CLASSES AS FINAL? ALMOST ALWAYS - FORCE DEVELOPERS

    THIKING ABOUT COMPOSITION
  22. HIGH QUALITY TESTS

  23. You will implore for LESS FEATURES

  24. STATIC ANALYSIS PHPSTAN & PHP INSIGHTS

  25. HOW HAVE HIGH QUALITY TESTS? ▸ Add 100% of real

    Test Coverage - PHPUnit ▸ Use and abuse Static Analysis - PHPStan & PHP Insights
  26. PLEASE CONSIDER THIS PRACTICES ▸ Open Source Libraries ▸ Important

    pieces of your software ▸ Long-lived projects
  27. EXPERIENCE

  28. None
  29. EFFECTIVE-PHP.COM PREMIUM VIDEO COURSE - GOOD CODE IS SHORT, SIMPLE,

    AND ROBUST. NUNOMADURO.COM Thanks!