"Better Code Design in PHP", Anna Filina

fwdays 211 views 57 slides Aug 19, 2024
Slide 1
Slide 1 of 57
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57

About This Presentation

Are you tired of spending hours reading code just to find the right place to make a small change? Are you forced to split your application into several just so that devs don't step on each other's toes? This talk unites theory with practice to show you how to structure your code so that it i...


Slide Content

Better Code
Design in PHP
PHP FWDAYS’24
@[email protected]

IStand With Ukraine
Anna Filina
•Coding since 1997.
•PHP, Java, C#, etc.
•Legacy archaeology.
•Test automation.
•Talks and workshops.
•YouTube videos.
•Filina Consulting.

IStand With Ukraine

IStand With Ukraine
#100

IStand With Ukraine
Object
Back-and-forth
Collaborator

IStand With Ukraine
Object
One-way reference
Collaborator

IStand With Ukraine
Object
Chatty
Collaborator
Step 1
Step 2
Step 3

IStand With Ukraine
Object
Less chatty
CollaboratorTask
Step 1
Step 2
Step 3

IStand With Ukraine
Is the complexity justified?
Object 1
Object 2
Object 3
Object 4
Bus

IStand With Ukraine
Design Drive
•Avoid complexity until I need it.
•Do I understand what’s going on in terms of the business use case?

IStand With Ukraine

IStand With Ukraine
Objects should speak business,
not technical.

IStand With Ukraine
$lineItems= $this->orders->getLineItems($pendingOrderNumber);
$taxTotals= $this->taxes->calculateForLineItems($lineItems);

IStand With Ukraine
Project
•Marketplace for short-term rental:
•Search properties for rent by city, date and number of guests.
•Reserve & pay.

IStand With Ukraine
ReservationService
->reserve()
PaymentManager
ReservationManager
processPayment(data)
Templating
saveReservation(data)
confirmReservation(data)
html
EmailManager
payment
savePayment(data)
confirmReservation(html)

IStand With Ukraine
ReservationService
->reserve()
PaymentManager
ReservationManager
processPayment(data)
Templating
saveReservation(data)
confirmReservation(data)
html
EmailManager
payment
savePayment(data)
confirmReservation(html)

IStand With Ukraine
MakeReservation ReservationRepository
add(paidReservation)
ReservationNotifier
PaymentProcessor
capture(charge)
paymentConfirmation
notifyBuyer(paidReservation)

IStand With Ukraine
MakeReservation ReservationRepository
add(paidReservation)
ReservationNotifier
PaymentProcessor
capture(charge)
paymentConfirmation
notifyBuyer(paidReservation)

IStand With Ukraine
MakeReservation ReservationRepository
add(paidReservation)
ReservationNotifier
PaymentProcessor
capture(charge)
paymentConfirmation
notifyBuyer(paidReservation)

IStand With Ukraine
MakeReservation ReservationRepository
add(paidReservation)
notifyBuyer(paidReservation)
ReservationNotifier
PaymentProcessor
capture(charge)
paymentConfirmation

IStand With Ukraine
MakeReservation ReservationRepository
add(paidReservation)
ReservationNotifier
PaymentProcessor
capture(charge)
paymentConfirmation
notifyBuyer(paidReservation)

IStand With Ukraine
Good design needs no diagrams.

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine
Simple Code
•No need for framework.
•No need to design the database.
•No need to have a UI.
•Code the business rules.

IStand With Ukraine

IStand With Ukraine
Handle HTTP Request
Return HTTP Response
2 adults
in Lviv
on Jan 1 to Jan 7
Results containing
“Lviv for 4”

IStand With Ukraine

IStand With Ukraine
Handle HTTP Request
Return HTTP Response
2 adults
in Lviv
on Jan 1 to Jan 7
Results containing
“Lviv for 4”

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine
A complex system that works is
invariably found to have evolved from a
simple system that worked.
—John Gall

IStand With Ukraine

IStand With Ukraine
Too reusable becomes unusable.

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine
Input/output Business rules InfrastructureUse case

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine

IStand With Ukraine
PropertySearchHandler
Lviv, Lviv Oblast, Ukraine
Convert to coordinates.
CitySearchHandler
Lviv
-Lviv, Lviv Oblast, Ukraine
-Lviv Boulevard, Oshawa, ON

IStand With Ukraine

IStand With Ukraine
PropertySearchHandler
49.8397, 24.0297 Longitude: -180 to +180
Latitude: -90 to +90
CitySearchHandler
Lviv
Lviv, Lviv Oblast, Ukraine
49.8397
24.0297

IStand With Ukraine

IStand With Ukraine
Code is simply a model of
the business rules and knowledge.