tags:

views:

272

answers:

2

PHP is driving me insane right now, please help me, I must be missing something. In this VERY BASIC example below 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;
 }
}
+3  A: 
<?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.

Chacha102
Thanks I am really going crazy for like an hour with this now, it's always something obvious it seems
jasondavis
+1  A: 

In other words, you have a syntax error:

function __construct() { <-- note the extra open curly
{ <-- note the extra open curly
Mr-sk