Last active
June 29, 2022 16:58
-
-
Save atakde/434b6ad8dbcfa83e3ebf42937e273b95 to your computer and use it in GitHub Desktop.
Static Factory Design Pattern Example With PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Customer | |
{ | |
public function hello() | |
{ | |
echo "Hello from customer!"; | |
} | |
} | |
class Admin | |
{ | |
public function hello() | |
{ | |
echo "Hello from admin!"; | |
} | |
} | |
class StaticFactory | |
{ | |
public static function build($type) | |
{ | |
return match ($type) { | |
'customer' => new Customer(), | |
'admin' => new Admin() | |
}; | |
} | |
} | |
$object = StaticFactory::build('customer'); | |
$object->hello(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment