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

The Final Word About final?

The Final Word About final?

In this talk, Stuart looks at PHP's `final` keyword, it's impact on type-hinting and code re-use, and how the SOLID principles can be used to strike a balance between code re-use and modification.

Presented at PHP South West in Bristol on Wednesday 8th November 2017.

Stuart Herbert

November 08, 2017
Tweet

More Decks by Stuart Herbert

Other Decks in Programming

Transcript

  1. @GanbaroDigital In This Talk 1. `final` and PHP type-hinting 2.

    An example of `final` in the wild 3. A SOLID approach
  2. @GanbaroDigital In This Talk 1. `final` and PHP type-hinting 2.

    An example of `final` in the wild 3. A SOLID approach
  3. @GanbaroDigital In This Talk 1. `final` and PHP type-hinting 2.

    An example of `final` in the wild 3. A SOLID approach
  4. @GanbaroDigital Class C1 extends Bar Class C2 extends Bar Class

    C3 extends Bar Class Foo Method doAction(Bar $input)
  5. @GanbaroDigital Class C1 extends Bar Class C2 extends Bar Class

    C3 extends Bar Class Foo Method doAction(Bar $input) ✓ ✓ ✓
  6. @GanbaroDigital ?? ?? What changes if we mark class Bar

    (our method type-hint) as `final`?
  7. @GanbaroDigital Class C1 extends Bar Class C2 extends Bar Class

    C3 extends Bar Class Foo Method doAction(Bar $input)
  8. @GanbaroDigital Class C1 extends Bar Class C2 extends Bar Class

    C3 extends Bar Class Foo Method doAction(Bar $input) ✗ ✗ ✗
  9. @GanbaroDigital PHP Fatal error: Class MyFoo may not inherit from

    final class (Foo) in test.php on line 19
  10. @GanbaroDigital Jimmy Nelson, The Cost of GUIDs as Primary Keys,

    March 8th, 2002 http://www.informit.com/articles/ article.aspx?p=25862
  11. @GanbaroDigital SOLID Principles • Single responsibility • Open/closed • Liskov

    substitution • Interface segregation • Dependency inversion
  12. @GanbaroDigital SOLID Principles • Single responsibility • Open/closed • Liskov

    substitution • Interface segregation • Dependency inversion
  13. @GanbaroDigital SOLID Principles • Single responsibility • Open/closed • Liskov

    substitution • Interface segregation • Dependency inversion
  14. @GanbaroDigital SOLID Principles • Single responsibility • Open/closed • Liskov

    substitution • Interface segregation • Dependency inversion
  15. @GanbaroDigital SOLID Principles • Single responsibility • Open/closed • Liskov

    substitution • Interface segregation • Dependency inversion
  16. @GanbaroDigital `final` Affects ...? • Single responsibility • Open/closed •

    Liskov substitution • Interface segregation • Dependency inversion
  17. @GanbaroDigital `final` Affects ... • Single responsibility • Open/closed •

    Liskov substitution • Interface segregation • Dependency inversion
  18. @GanbaroDigital Class C1 extends Bar Class C2 extends Bar Class

    C3 extends Bar Class Foo Method doAction(Bar $input) ✗ ✗ ✗
  19. @GanbaroDigital Liskov substitution principle: type T may be substituted by

    a sub-type S without altering the desirable properties of the program.
  20. @GanbaroDigital SOLID Principles • Single responsibility • Open/closed • Liskov

    substitution • Interface segregation • Dependency inversion
  21. @GanbaroDigital Class C1 extends Bar Class C2 extends Bar Class

    C3 extends Bar ✗ ✗ ✗ Class Bar (Interface1)
  22. @GanbaroDigital Class C1 (Interface1) Class C2 (Interface1) Class C3 (Interface1)

    Class Foo Method doAction(Interface1 $input) ✓ ✓ ✓
  23. @GanbaroDigital Class C1 extends Bar Class C2 extends Bar Class

    C3 extends Bar ✗ ✗ ✗ Class Bar (Interface1)