-1

I am trying to implement singleton pattern for a class. I have a static variable $classInstance and when assigning class object getting the error "Parse error: syntax error, unexpected '='".

  /**
   * Hold instance of class.
   */
  private static $classInstance = null;

  /**
   * Create the client connection.
   */
  public static function createClient() {
    if (self::classInstance === null) {
      self::classInstance = new self(); // Getting error on this line.
    }
    return self::classInstance;
  }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Akansha
  • 132
  • 8

1 Answers1

0

The error was because of missing "$" while accessing static property of class with self keyword.

  /**
   * Create the client connection.
   */
  public static function createClient() {
    if (self::$classInstance === null) {
      self::$classInstance = new self(); // Note the $ sign.
    }
    return self::$classInstance;
  }
Akansha
  • 132
  • 8