title: Adapter pattern with static classes
body: <p>I am looking for a good way to implement the Adaptor pattern with static classes in PHP 5.x.</p>
  
  <p>One of the examples where I would like to use this, is as a counterpart to Python's <code>os.path.join()</code>.
  I would have two adaptees, a Windows and a Linux adaptee class.</p>
  
  <p>I think it is reasonable, to implement these classes as static classes, because they have no "context". They do not need to store any state and creating an instance everytime I need one seems superfluous - therefore I am looking for a clean way to implement this.</p>
  
  <p>Let's consider the following bogus implementation:</p>
  
  <pre><code>    static public function join(){
          $parts = func_get_args();
          $joined = array(MY_MAGICALLY_PASSED_DEFAULT_PATH_PREFIX);
          foreach($parts as $part){
              $part = self::$adaptee-&gt;cleanPath($path);
              if(self::$adaptee-&gt;isAbsolute($part)){
                  $joined = array($part);
              }
              else{
                  $joined[] = $part;
              }
          }
          return implode(PATH_SEPARATOR, $joined);
      }
  </code></pre>
  
  <p>The first thing you will notice is, that it assumes an initialized static member called adaptee which would hold the necessary, OS-dependent implementation details.</p>
  
  <p>This requires me to have an arbitrarily named static constructor-like function, that I would call immediately after the declaration of the class. (Another thing that bothers me with this approach).</p>
  
  <p>Of course, I could initialize a local <code>$adaptee</code> variable on each method call, but that seems like inappropriate and I would have to  replicate that in each other static function that needs the adaptee.</p>
  
  <p>Now... for PHP's class implemention detail: They are not first-class objects, so I couldn't just pass the class as an argument. In the example, it requires me to create the Adaptees as <em>non-static</em> (what is the term for this?) classes, then instantiate it and eventually assign it to the static <code>$adaptee</code> member variable of the Adapter class.</p>
  
  <p>Maybe this is just this weird and completely subjective thought that I have... but I really feel that it is not appropriate to do it like this. Do you have any ideas about a better implementation?</p>
  
  <p>One other idea that I've had is, to store the adaptee's class name instead, and use <code>call_user_func</code> instead, but I don't feel too comfortable using this approach.</p>
  
  <h1>Update</h1>
  
  <p>I may not have described this properly, so I will try to explain this in an update:</p>
  
  <p>I am not looking on how to get the underlying Operating System, but I would like to have a neat way, for a static class to act differently depending on whether the OS is Linux, Windows, FreeBSD or something else.</p>
  
  <p>I thought of the adaptor pattern, but since I don't have a static constructor, I cannot really initialize the class. One way would be to initialize it at the beginning of every public static method call (or just check whether it is initialized).</p>
  
  <p>The other possibility would be, to create a static constructor-like method and simply call it right after the declaration. That might do the trick, but I am just wondering what other, possibly more elgeant methods there are, to achieving this.</p>
  
  <p>As for my initial example:
  It is supposed to be a utility function, it does not need to preserve state in any kind really, so I am not looking for a Path-Object of any sorts. What I would like, is a Path factory function, that returns a string, without having to differentiate between the different OSes every time when called. The "library"-thing led me to create a static class as pseudo-namespace for my related utility functions, and the different implementation details that need to be supported to the adaptor pattern. Now I am looking for an elegant way, to combine the two.</p>