Skip to main content
1 of 2
jhilgeman
  • 231
  • 1
  • 4

Looks sort of like you're trying to accomplish a singleton pattern, using the App class to control them all. Try this approach instead:

<?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->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->FieldY = "Foo";
  }

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


// Usage examples
echo MyProvider::getInstance()->FieldX; // Hello World!
echo MyOtherProvider::getInstance()->FieldY; // Foo
echo MyOtherProvider::getInstance()->MakeMoreExciting("Hi there"); // Hi there!!!!!!!!
jhilgeman
  • 231
  • 1
  • 4