开发者

Troubleshooting unexpected T_PUBLIC error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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;
 }
}


<?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.


In other words, you have a syntax error:

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


I had the same Problem and then i found out that the curly braces "{" of my class was missing .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜