dimanche 7 février 2016

Get list of all the implementations of an interface using factory pattern in php

I have an interface called MyModule.php and few classes Module1.php,Module2.php,Module3.php which implements the MyModule interface. The objective is to use a factory pattern approach to get a list of all the implementations of the MyModule interface. Should I create a MyFactory.php class in the same folder and use something like

$classes = get_declared_classes();
$implementsMyModule = array();
foreach($classes as $cs) {
    $reflect = new ReflectionClass($cs);
    if($reflect->implementsInterface('MyModule')) 
        $implementsMyModule[] = $cs;
 }

Is this the correct way to do this?

Aucun commentaire:

Enregistrer un commentaire