1

I'm curious to know how fellow developers manage project agnostic code/libraries?

For example, given these two functions:

function array_exclude_keys(Array $array, Array $keys){
    foreach($keys as $key){
        unset($array[$key]);
    }
    return $array;
}

function array_order_to_assoc(Array $array){
    do{
        $return[current($array)] = next($array);
    }while(next($array));
    return !empty($return) ? $return : null;
}

These have a pretty general application. In a project I'm currently working on, array_exclude functionality is needed in at least two places, and array_order_to_assoc in at least one. I can even think of other projects which would benefit semantically from these.

Now, instead of writing them into methods for the necessary classes (non-DRY), I could simply amend them to a library, and include that library in the project. However, now the classes requiring these functions are completely dependent. Without such dependency, my classes in this project are quite literally "copy, paste, include, and instantiate", which is how I'd like to keep them.

I'm curious to know how others manage such situations.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174

1 Answers1

1

If I'm understanding your question correctly, it sounds like you should be looking into php's spl_autoload functionality.

Here is a tutorial: http://www.phpro.org/tutorials/SPL-Autoload.html

vicTROLLA
  • 1,554
  • 12
  • 15
  • **Thanks vicTROLLA;** That's not exactly what I'm on about, I'm already using the core `__autoload()` for autoloading (while SPL is more powerful, it's simply not necessary in this project) Some of the classes in this project can function completely independent of one another, loose coupling at it's finest perhaps. My `Router` being one of them, however requires the aforementioned functions, but so do other classes. My choices as I see it, are repeating the necessary methods in each class, or loading them in an external library. I'm just curious about alternatives to maintain loose coupling. – Dan Lugg Mar 11 '11 at 03:44