mercredi 14 décembre 2016

How to implement a factory pattern using php composer and namespaces

I want to implement a factory pattern to php. At the moment I have each class has a different name and is loaded via require (I use the composer but I did not realize all goes well). The system is the management of smart home, I want to have each had their devaysa controller.php (class name and the file) but I do not know how I will address in my desired namespace if the factory trying to use "use namespace" I receive an error. Is it possible to realize what I want or every class must have a unique file and therefore a unique class name

/device/light/1/controller.php

namespace smarthouse\devices\light\1;

class controller extends smarthouse\controllers\device implements smarthouse\interfaces\deviceInterface {

    public function __construct($ip)
    {
        parent::__construct();
    }

    public function auth(){}
    public function reboot(){}

    }

/device/light/2/controller.php

...same

/controllers/routerFactory.php

namespace smarthouse\controllers;

    public static function factory($device)
    {   
          //some code
        $object =  new controller($device); 
        return $object;
    }

Aucun commentaire:

Enregistrer un commentaire