title: How can I call a static method on a variable class?
body: <p>I'm trying to make some kind of function that loads and instantiates a class from a given variable. Something like this:</p>
  
  <pre><code>&lt;?php
  function loadClass($class) {
    $sClassPath = SYSPATH."/classes/{$class}.php";
    if (file_exists($sClassPath)) {
      require_once($sClassPath);
      $class = $class::getInstance();
    }
  }
  ?&gt;
  </code></pre>
  
  <p>If I use it like this:</p>
  
  <pre><code>&lt;?php
    loadClass('session');
  ?&gt;
  </code></pre>
  
  <p>It should include and instantiate the session class.</p>
  
  <p>BTW: the static getInstance function comes from this code:</p>
  
  <pre><code>&lt;?php
    function getCallingClass() {
      $backtrace = debug_backtrace();
      $method    = $backtrace[1]['function'];
      $file      = file($backtrace[1]['file']);
      $line      = $file[($backtrace[1]['line'] - 1)];
      $class     = trim(preg_replace("/^.+?([A-Za-z0-9_]*)::{$method}\(.*$/s", "\\1\\2", $line));
  
      if(! class_exists($class)) {
        return false;
      } return $class;
    }
  
    class Core {
  
      protected static $instances = array();
  
      public static function getInstance() {
        $class = getCallingClass();
  
        if (!isset(self::$instances[$class])) {
          self::$instances[$class] = new $class();
        } return self::$instances[$class];
      }
  
    }
  
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p>The thing is that right now the way to use the functions in a class is this:</p>
  
  <pre><code>&lt;?php
    $session = session::getInstance();
  ?&gt;
  </code></pre>
  
  <p>But now I want to build that into a function so that I never again have to use that line of code.
  I just say loadClass('session');
  and than I can use $session->blablablafunction();</p>