I have various classes, e.g.
<?php
namespace MyApp\Notifications;
class FirstNotification implements NotificationInterface {
public function getMessage() {
return 'first message';
}
}
and
<?php
namespace MyApp\Notifications;
class SecondNotification implements NotificationInterface {
public function getMessage() {
return 'second message';
}
}
I then have an array like: ['First','Second'].
I'm using:
foreach (['First','Second'] as $class_prefix) {
$class = "MyApp\Notifications\\{$class_prefix}Notification";
$object = new $class();
echo $object->getMessage();
}
but it feels a bit hacky - is there a better/more standard way to do this? The array is supplied elsewhere and will be different depending on the user - my aim is to be able to easily create classes that implement my interface and know this loop will be able to show their messages if they exist.
I ideally don't want to have to add a use statement for all the classes upfront, or pass them into a constructor, I just want magic to happen!
MyAppthough)