php 意外的 T_VARIABLE,期待 T_FUNCTION
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6486660/
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
unexpected T_VARIABLE, expecting T_FUNCTION
提问by Ryan Leonard
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.
我已经为此苦苦挣扎了一段时间,只是想知道是否有人可以发现我出错的地方。
回答by Sabeen Malik
You can not put
你不能放
$connection = sqlite_open("[path]/data/users.sqlite", 0666);
$connection = sqlite_open("[path]/data/users.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.
在班级建设之外。您必须将该行放在函数或构造函数中,但不能将其放置在现有位置。
回答by Lekensteyn
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:
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.
根据您的需要,protected
或者private
可能是$connection
. 这可以防止您意外关闭或弄乱连接。
回答by duri
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);
}
回答by Stijn Leenknegt
put public, protected or private before the $connection.
在 $connect 之前放置 public、protected 或 private。