Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshooting unexpected T_PUBLIC error [closed]

Tags:

php

I get this error...

Parse error:syntax error, unexpected T_PUBLIC in C:\filename here on line 12

On this line....

public static function getInstance(){

The code...

<?PHP
class Session{

 private static $instance;

 function __construct() {
 {
  session_start();
  echo 'Session object created<BR><BR>';
 }

 public static function getInstance(){
  if (!self::$instance) {
   self::$instance = new Session();
  }
  return self::$instance;
 }
}
like image 619
JasonDavis Avatar asked Dec 18 '22 03:12

JasonDavis


1 Answers

<?PHP
class Session{

    private static $instance;

    function __construct() 
    {
        session_start();
        echo 'Session object created<BR><BR>';
    }

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new Session();
        }
        return self::$instance;
    }
}

Try that. You had an extra bracket.

The error was actually in the line function __construct(). It created a function and then an empty set of brackets (doesn't actually error).

Then, you never ended up breaking out of the construct function so it error-ed when you tried to use the public parameter inside a function, which is not valid syntax.

This is why we make consistent bracket placement, so we always put stuff in the same place, and thus can easily spot mis-placements.

like image 63
Tyler Carter Avatar answered Dec 30 '22 18:12

Tyler Carter