Wiring classes together in PHP I've created a base structure for my project and found a way to wire classes together, I create an array of previously loaded classes and call them from the cache. I then have a static class that holds the cache and I call the static class when I need a class.

I feel that there is a much better way to do this, and maybe I'm doing something extremely wrong? How do other PHP applications do this? I'm pretty new to it, so I was wondering if anyone could advise me towards a better way to do this? It feels like it could be hugely improved.

Current directory structure:



The static class, I use this to get my classes (providers, I call them):

&lt;?php declare(strict_types = 1);

namespace App;

class App {
    private static $providers = array();

    public static function getProvider($provider) {
        if (!isset(self::$providers[$provider])) {
            $providerName = 'App\Providers\\' . $provider;
            self::$providers[$provider] = new $providerName();
        }

        return self::$providers[$provider];
    }
}


Example of how I use it, I do this in all my controllers to show a view:

&lt;?php declare(strict_types = 1);

use App\App;

namespace App\Controllers\Frontend\Guest;

class LoginController 
{
    public function getView() 
    {
        \App\App::getProvider('Template')-&gt;renderTemplate('index.html');
    }
}


Here is one of my few providers:

&lt;?php declare(strict_types = 1);

namespace App\Providers;

class Template {
    private $twig;

    public function __construct() {
        $this-&gt;twig = new \Twig_Environment(new \Twig_Loader_Filesystem(ROOT . '/resources/templates'), array(
            'cache' =&gt; ROOT . '/storage/cache/templates',
        ));
    }

    public function renderTemplate($template) {
        echo $this-&gt;twig-&gt;render($template, array('the' =&gt; 'variables', 'go' =&gt; 'here'));
    }
}
Looks sort of like you're trying to accomplish a singleton pattern, using the App class to control them all. Try this approach instead:

&lt;?php
// Base / abstract class that defines a basic singleton pattern
abstract class SingletonProvider
{
  // Singleton pattern
  protected static $_instance;  
  public static function getInstance()
  {
    if(static::$_instance == null)
    {
      static::$_instance = new static();
    }
    return static::$_instance;
  }

  // Private constructor
  private function __construct() { }
}


// A specific provider class definition that inherits the singleton pattern
class MyProvider extends SingletonProvider
{
  // Class-specific instance reference
  protected static $_instance;  

  // --- CLASS INSTANCE-SPECIFIC PROPERTIES AND METHODS BELOW HERE ---

  // Class properties
  public $FieldX;

  protected function __construct()
  {
    $this-&gt;FieldX = "Hello World!";
  }
}


// Another specific provider class definition that inherits the singleton pattern
class MyOtherProvider extends SingletonProvider
{
  // Class-specific instance reference
  protected static $_instance;  

  // --- CLASS INSTANCE-SPECIFIC PROPERTIES AND METHODS BELOW HERE ---

  // Class properties
  public $FieldY;

  protected function __construct()
  {
    $this-&gt;FieldY = "Foo";
  }

  public function MakeMoreExciting($sentence)
  {
    return $sentence . "!!!!!!!!";
  }
}


// Usage examples
echo MyProvider::getInstance()-&gt;FieldX; // Hello World!
echo MyOtherProvider::getInstance()-&gt;FieldY; // Foo
echo MyOtherProvider::getInstance()-&gt;MakeMoreExciting("Hi there"); // Hi there!!!!!!!!


The "static" keyword is used for late static binding, which basically means that when you run MyOtherProvider::getInstance(), all the "static" references like "static::$_instance" resolve to the calling class, so it's like automatically determining either MyOtherProvider::$_instance or MyProvider::$_instance, depending on which class is being called.

The protected static $_instance is there to ensure that each class has its own property to hold the class instance. 

In theory, you could even shorten all of this up into nothing but static methods and properties, too:

abstract class MyOtherProvider
{
  // Class properties
  public static $FieldY;

  public static function MakeMoreExciting($sentence)
  {
    return $sentence . "!!!!!!";
  }
}

echo MyOtherProvider::MakeMoreExciting("Hi there"); // Hi there!!!!!!


Although sometimes you might want an actual instance rather than just being completely static.