Skip to content

Instantly share code, notes, and snippets.

@atakde
Last active June 29, 2022 16:58
Show Gist options
  • Save atakde/434b6ad8dbcfa83e3ebf42937e273b95 to your computer and use it in GitHub Desktop.
Save atakde/434b6ad8dbcfa83e3ebf42937e273b95 to your computer and use it in GitHub Desktop.
Static Factory Design Pattern Example With PHP
<?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