Extremely Defensive PHP
Above is a talk from Marco (Ocramius) on extremely defensive PHP. Marco works at a company called Roave (software consultancy), and is an active contributor in th Zend Framework and Doctrine teams. He conveys a lot of great points that I think we could all benefit from.
Timestamps
- Introduction to speaker (00:00)
- Aggressive practices for long lived or open source projects (01:13)
- We are dumb - we don't know what are going to need to implement in 3 months time (01:55)
- Defensive driving - driving assuming others will make mistakes (02:40)
- Poka yoke - build something so that you can use it only in one way (03:42)
- RJ45 Adapter - good design (04:10)
- USB Adapter - bad design (04:40)
- Code is not reusable. (05:10)
- Abstractions are reusable (05:46)
- Make State Immutable - PSR 7 got it right (06:50)
- What about performance? (07:19)
- No setters (07:40)
- The Constructor is the only injection point (08:04)
- No Uninitialized properties. (08:43)
- Optional Dependencies? (08:55) ▸
- Avoid unnecessary public methods. (10:03)
- Avoid logic switch parameters (10:52)
- All state should be encapsulated (11:47)
- External access includes subclasses (13:52)
- Make state private (14:20)
- Private properties by default (14:25)
- Assert - enforce strict invariants - php7 array with a type (14:51)
- Beberlei/Assert library for asserting (16:05)
- No mixed parameter types and value object example (16:35)
- Value Objects FTW (17:45) ▸
- No mixed return types - could just use a value object (18:33)
- Avoid fluent interfaces (18:54)
- Make classes final by default (20:39)
- An extension is a different use-case (20:55)
- Does extending the interface add any meaning? (21:25)
- Ensure Deep Cloning (22:30)
- Disable cloning? (23:26)
- Is it really serializable? (24:13)
- Unit testing - test all scenarios (25:24)
- Trust no code (27:34)
- Reduce features to the bare minimum - YAGNI (27:48)
Last updated: 13th April 2023
First published: 16th August 2018
First published: 16th August 2018