意想不到的T_VARIABLE,期待T_FUNCTION

时间:2021-04-27 22:44:33

I am expecting this to be a basic syntax error I overlooked, but I can't figure it out.

我希望这是我忽略的一个基本语法错误,但是我不能理解它。

In a PHP script, I keep getting the following error.

在PHP脚本中,我一直得到以下错误。

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in [path]/scripts/users/database_connection.php on line 4

This occurs when my script to connect to the database is called with an include_once(). I stripped my script down to the most basic code (leaving in what is required by other code), and it still is calling this error.

当我的脚本连接到数据库时,会调用include_once()。我将我的脚本删除到最基本的代码(在其他代码需要的代码中),它仍然调用这个错误。

<?php
    class UserDatabaseConnection
    {
        $connection = sqlite_open("[path]/data/users.sqlite", 0666);
        public function lookupUser($username)
        {
            // rest of my code...
        }
    }

    $udb = new UserDatabaseConnection;
?>

I have struggled with this for a while, and just wondered if anyone else could spot somewhere I went wrong.

我为此挣扎了一段时间,只是想知道有没有人能发现我哪里出了问题。

4 个解决方案

#1


18  

You can not put

你不能把

$connection = sqlite_open("[path]/data/users.sqlite", 0666);

连接= sqlite_open(美元”(路径)/数据/用户。sqlite ",0666);

outside the class construction. You have to put that line inside a function or the constructor but you can not place it where you have now.

外的类结构。你必须把这条线放在函数或构造函数中,但你不能把它放到现在的位置。

#2


16  

You cannot use function calls in a class construction, you should initialize that value in the constructor function.

在类构造中不能使用函数调用,应该在构造函数函数中初始化该值。

From the PHP Manual on class properties:

关于类属性的PHP手册:

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

这个声明可能包含一个初始化,但是这个初始化必须是一个常量值——也就是说,它必须能够在编译时进行评估,并且不能依赖于运行时信息来进行评估。

A working code sample:

一个工作代码示例:

<?php
    class UserDatabaseConnection
    {
        public $connection;
        public function __construct()
        {
            $this->connection = sqlite_open("[path]/data/users.sqlite", 0666);
        }
        public function lookupUser($username)
        {
            // rest of my code...
            // example usage (procedural way):
            $query = sqlite_exec($this->connection, "SELECT ...", $error);
            // object oriented way:
            $query = $this->connection->queryExec("SELECT ...", $error);
        }
    }

    $udb = new UserDatabaseConnection;
?>

Depending on your needs, protected or private might be a better choice for $connection. That protects you from accidentally closing or messing with the connection.

根据您的需要,保护或私有可能是$connection的更好选择。这可以保护你不意外地关闭或干扰连接。

#3


4  

Use access modifier before the member definition:

在成员定义之前使用访问修饰符:

    private $connection;

As you cannot use function call in member definition in PHP, do it in constructor:

由于您不能在PHP中使用成员定义中的函数调用,请在构造函数中执行它:

 public function __construct() {
      $this->connection = sqlite_open("[path]/data/users.sqlite", 0666);
 }

#4


3  

put public, protected or private before the $connection.

将公共、受保护或私有置于$连接之前。

#1


18  

You can not put

你不能把

$connection = sqlite_open("[path]/data/users.sqlite", 0666);

连接= sqlite_open(美元”(路径)/数据/用户。sqlite ",0666);

outside the class construction. You have to put that line inside a function or the constructor but you can not place it where you have now.

外的类结构。你必须把这条线放在函数或构造函数中,但你不能把它放到现在的位置。

#2


16  

You cannot use function calls in a class construction, you should initialize that value in the constructor function.

在类构造中不能使用函数调用,应该在构造函数函数中初始化该值。

From the PHP Manual on class properties:

关于类属性的PHP手册:

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

这个声明可能包含一个初始化,但是这个初始化必须是一个常量值——也就是说,它必须能够在编译时进行评估,并且不能依赖于运行时信息来进行评估。

A working code sample:

一个工作代码示例:

<?php
    class UserDatabaseConnection
    {
        public $connection;
        public function __construct()
        {
            $this->connection = sqlite_open("[path]/data/users.sqlite", 0666);
        }
        public function lookupUser($username)
        {
            // rest of my code...
            // example usage (procedural way):
            $query = sqlite_exec($this->connection, "SELECT ...", $error);
            // object oriented way:
            $query = $this->connection->queryExec("SELECT ...", $error);
        }
    }

    $udb = new UserDatabaseConnection;
?>

Depending on your needs, protected or private might be a better choice for $connection. That protects you from accidentally closing or messing with the connection.

根据您的需要,保护或私有可能是$connection的更好选择。这可以保护你不意外地关闭或干扰连接。

#3


4  

Use access modifier before the member definition:

在成员定义之前使用访问修饰符:

    private $connection;

As you cannot use function call in member definition in PHP, do it in constructor:

由于您不能在PHP中使用成员定义中的函数调用,请在构造函数中执行它:

 public function __construct() {
      $this->connection = sqlite_open("[path]/data/users.sqlite", 0666);
 }

#4


3  

put public, protected or private before the $connection.

将公共、受保护或私有置于$连接之前。