DEV Community

Cover image for Design Patterns in PHP 8: Simple factory

Design Patterns in PHP 8: Simple factory

Max Zhuk on July 29, 2023

Hello, fellow developers!πŸ§‘πŸΌβ€πŸ’» Today, we're going to delve into the Simple Factory pattern. This pattern, while not officially recognized as a form...
Collapse
 
suckup_de profile image
Lars Moelleken • Edited

Thanks for this post.

PS: we should try to prevent strings because we have much better support for symbols by the IDE + static code analysis

$factory->createTransport('truck')
Enter fullscreen mode Exit fullscreen mode

vs.

$factory->createTransport(Truck::class)
Enter fullscreen mode Exit fullscreen mode

+

/*
 * @param class-string<Transport>
 */
public function createTransport($type): Transport {
...
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zhukmax profile image
Max Zhuk • Edited

Thanks, I agree that the code will be better this way

Collapse
 
goodevilgenius profile image
Dan Jones

Types on all the methods. If you're doing PHP 8, all the arguments and returns should be typed.