Principles of PHP Package DesignObject oriented design for packages
Matthias Noback - Noback's OfficeDomCode, first monthly meeting - 7/28/2014
First monthly meeting of DomCode
2/52
Also: congratulations to SweetlakePHP
3/52
Matthias NobackNoback's Office
PHP developerWriter/speakerProud father
·
·
·
4/52
Tight couplingCoupling to a framework
7/52
Code
8/52
Squiggly lines
9/52
Code
10/52
PackagesThere are many different kinds
11/52
Class design
12/52
Package designNothing?
butunclebob.com
13/52
Principles of PHP Package Design
leanpub.com/principles-of-php-package-design
14/52
Package designCohesion
15/52
Package designCoupling
16/52
A - Cohesion principlesPerspective: the package in isolation
17/52
1 - The Release Reuse Equivalence PrincipleThe granule of reuse is the granule of release
18/52
1 - The Release Reuse Equivalence Principle
Version control and hosting
Composer package definition
Meta-files
Auto-loading
·
·
·
·
Semantic versioning
Quality control
·
BranchesTagsBackward compatibility
-
-
-
·
19/52
1 - The Release Reuse Equivalence Principle
If you don't have the time to turn your reusable code into a proper package...
Don't release it.
20/52
2- The Common Reuse PrincipleClasses that are used together are packaged together
If you use one class of a package,you will use all its other classes too.
21/52
2- The Common Reuse PrincipleSmell: Feature strata
22/52
2- The Common Reuse PrincipleExample of feature strata: the Symfony Security Component
23/52
2- The Common Reuse PrincipleSmell: Classes with different dependencies
24/52
2- The Common Reuse PrincipleExample of different dependencies: Gaufrette
25/52
2- The Common Reuse PrincipleDifferent dependencies: Gaufrette
{ "name": "knplabs/gaufrette", ... "suggest": { "knplabs/knp-gaufrette-bundle": "to use with Symfony2", "dropbox-php/dropbox-php": "to use the Dropbox adapter", "rackspace/php-opencloud" : "to use Opencloud adapter", "herzult/php-ssh": "to use SFtp adapter", "phpseclib/phpseclib": "to use PhpseclibSftp adapter", "aws/aws-sdk-php": "to use the Amazon S3 adapter", "amazonwebservices/aws-sdk-for-php": "to use the legacy Amazon S3 adapters", "doctrine/dbal": "to use the Doctrine DBAL adapter", "microsoft/windowsazure": "to use Microsoft Azure Blob Storage adapter", "ext-zip": "to use the Zip adapter", "ext-apc": "to use the APC adapter", ... }, ...} 26/52
2 - The Common Reuse PrincipleLeszek Prabucki's response
27/52
3 - The Common Closure PrincipleClasses that change together are packaged together
28/52
3 - The Common Closure PrincipleReasons for change
The application's features changeThe business rules changeThe web framework's best practices changeThe persistence library's configuration changes...
·
·
·
·
·
29/52
3 - The Common Closure PrincipleSmell: code for multiple application layers
Web User InterfaceCommand-line interfaceModelInfrastructure services
·
·
·
·
30/52
B - Coupling principlesPerspective: the package in relation to other packages
31/52
4 - The Acyclic Dependencies PrincipleThe dependency graph of packages must have no cycles
32/52
4 - The Acyclic Dependencies Principle
33/52
Stability
Something is stable if it's resistant to change.
34/52
5 - The Stable Dependencies PrincipleAn irresponsible package
35/52
5 - The Stable Dependencies PrincipleA dependent package
36/52
5 - The Stable Dependencies PrincipleAn instable package: irresponsible and dependent
37/52
5 - The Stable Dependencies PrincipleA responsible package
38/52
5 - The Stable Dependencies PrincipleAn independent package
39/52
5 - The Stable Dependencies PrincipleA stable package: responsible and independent
40/52
5 - The Stable Dependencies PrincipleDepend in the direction of stability
41/52
5 - The Stable Dependencies PrincipleCounter example
42/52
6 - The Stable Abstractions PrincipleWhat is more likely to change?
Something concrete or something abstract?A class or an interface?
·
·
43/52
6 - The Stable Abstractions PrincipleAbstractness should increase with stability
44/52
Summary
Reuse/release equivalence principle
Common reuse principle
Common closure principle
Acyclic dependencies principle
Stable dependencies principle
Stable abstractions principle
·
Reuse only code that you can release as a product.-
·
All code in a package is reused at the same time.-
·
Code in a package only changes for a few reasons.-
·
No cycles in the dependency graph.-
·
Only depend on more stable packages.-
·
More stable packages are also more abstract.-
45/52
Word of advice
You can't maximize them all at the same time.
Keep them in mind while you are working on a package.
46/52
Principles of PHP Package Design
leanpub.com/principles-of-php-package-design
I'm impressed. — Robert C. Martin
47/52
Principles of PHP Package Design
Get a 10 dollar discount:
http://leanpub.com/principles-of-php-package-design/c/domcode
48/52
Questions?
feedback joind.in/11580
twitter @matthiasnoback
leanpub leanpub.com/principles-of-php-package-design
49/52
Image courtesy
Clean CodersGitHubBitBucketPackagistPoEAABut Uncle BobRobert Martin
·
·
·
·
·
·
·
50/52
52/52
Top Related