php 排除意外的 T_PUBLIC 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2114439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:05:21  来源:igfitidea点击:

Troubleshooting unexpected T_PUBLIC error

php

提问by JasonDavis

I get this error...

我收到这个错误...

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

解析错误:语法错误,第 12 行 C:\filename 中的意外 T_PUBLIC

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

回答by Tyler Carter

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

错误实际上是在行中function __construct()。它创建了一个函数,然后创建了一组空括号(实际上并没有错误)。

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.

这就是为什么我们保持一致的支架放置,所以我们总是把东西放在同一个地方,因此很容易发现错误的放置。

回答by mr-sk

In other words, you have a syntax error:

换句话说,你有一个语法错误:

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

回答by shirin

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

我遇到了同样的问题,然后我发现我班级的花括号“{”不见了。