mardi 22 septembre 2015

Abstract, Interface and desing patterns dudes

I need some help to understand, or know better the way to do certains things in PHP. I develop to many years in PHP, using a small home-made MVC framework in PHP 5, never use PHP 5.2+ advantages because some proyects stay in old red hat machines and i forced to use php <5.2

Now i free of this proyects and have some shadows in how many ways I need to implement some PHP features.

I know what is a abstract class, and interface, and know the namespaces, and some desingn patters, but always struck in one simple thing, when is better use interfaces, abstract class or pattern design.

I put a real example, in my Framework i have a Core with Router class, the Router Calls the Core and the Core load the Controller and call the controller function using the Router vars. The Controler extends the Core, and can use a function in the Core to load "components", this function its a simple singleton pattern, using a Static array, to instance the classes i "call" from the controller or others components, this is extreme fast and use low memory, but i donw know if is the better way.

In the Controller or Component i simply write:

function example() {

    $this->loadComponent(array('Cache', 'Template', 'Email'));
    $this->Email->X();

}

This create the instances (if not exists, if exists return a pointer "&" tho the instance, not a copy) and set to the controller or component to allow me to use using $this->ComponentName->XXXX The function allow to create another copys, using $this->loadComponent(array('Cache' => 'Cache2')); if I need some copys and not the same. (for example for multiple DB connections)

I think this can be made better, but dont know how made it.

Now i struck in another desing problem:

I have an Cache class, this class have 3 ways of cache, Memcache, Redis or File Cache. The class its a simple class (no abstract or interface), and every cache functions are in separated class CacheMem, CacheRes, CacheFile, when the Cache class is loaded using loadComponent, the class read a define config, and using this define do this:

function __construct() {

    private $engine;

    switch (CONFIG_CACHE_TYPE) {

        case "MEM":
        $class = 'CacheMem'
        require ('Components'.DS.$class.'.php');
        $this->engine = new CacheMem();

        break;

        case "RES":
        $class = 'CacheRedis'
        require ('Components'.DS.$class.'.php');
        $this->engine = new CacheRedis();

        break;

        default:
        $class = 'CacheFile';
        require ('Components'.DS.$class.'.php');
        $this->engine = new Cachefile();
        break;

    }

}

function read($key) {
    $this->engine->read($key);
}

function write($key, $value, $time=3800) {
    $this->engine->write($key, $value, $time=3800);
}

I think the Cache class are shit but i donw know waht its the better way to solve this, its a simple simple problem, but i struck in one thing : i need to load the cache Class using the "Cache" name NOT CacheMem, CacheFile or CacheRes?

I try using abstract class Cache, and extends with the Mem, Res or File, but i need to instance in the Cache class the children because i what to use "Cache" not "CacheXXX", and i know its wrong.

How do you recommend solve this?

Thanks for the help.

Aucun commentaire:

Enregistrer un commentaire