samedi 3 juin 2017

PHP MVC How to inject/pass/design utility classes? Do I really need them?

In my HMVC project I've implemented an Arrays class. Its STATIC methods are used overall to handle various operations on multidimensional arrays. Beside this class I use other utility classes too (Files, Images, Printer, Messages, Classes, etc).

My question is based on the fact, that I don't want any statics or singletons in my project (these classes are the last statical part in it):

How can I use, or inject, or structure, or distribute this kind of utility classes (or functionalities) in my project without using them as static classes?

Of course, I can inject them in constructors/setters/methods - I already use a DIC too, but my question is more like a... design-pattern one, maybe: Should an Arrays, Images, Files class really exist? Should they really be injected?, etc...

I'm really hopping, that you can give me some solution, direction, idea, or tip, no matter how small. I maybe just need another perspective.

Thank you very much!


P.S:

Here is the Arrays class, if you need to take a look in it:

<?php

namespace MYMVC\Utils;

/**
 * Arrays: utility class to handle operations on multidimensional arrays.
 */
class Arrays {

    /* ------------------------------------------------------------- */
    /* Keypath format: '{key0}/{key1}/{key2}/{key3}/{searched-key}'. */
    /* ------------------------------------------------------------- */

    /** Set the value in array at the specified keypath. [...] */

    public static function setArrayValue(&$array, $keypath, $value, $delimiter = '/') { ... }


    /** Get a tree structure from the specified keypath. [...] */

    public static function buildArrayTree($keypath, $value, $delimiter = '/') { ... }


    /** Check if the specified keypath exists in array. [...] */

    public static function arrayKeyExists(&$array, $keypath, $delimiter = '/') { ... }


    /** Get the array value from the specified keypath. [...] */

    public static function getArrayValue(&$array, $keypath, $delimiter = '/') { ... }


    /** Merge multiple multidimensional arrays. [...] */

    public static function mergeMultipleArrays(/* func_get_args */) { ... }


    /** Merge the top multidimensional array over the base multidimensional array. [...] */

    public static function mergeArrays($base, $top) { ... }

}

Aucun commentaire:

Enregistrer un commentaire