php 解析错误:语法错误,意外的 T_VARIABLE,期待 T_FUNCTION
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10401722/
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
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
提问by user1232117
Could someone tell me what I'm doing wrong?
有人能告诉我我做错了什么吗?
I want to display the users online on specific rooms only.
我只想在特定房间在线显示用户。
the code below is the function that calls my online.php this is under my chat.php when I load the page this function also loads.
下面的代码是调用我的 online.php 的函数,当我加载页面时,这个函数也在我的 chat.php 下。
function whos_online() {
if ( window.XMLHttpRequest ) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "online.php?room=<?php $_SESSION['room']?>", false);
xmlhttp.send();
document.getElementById("whos_online").innerHTML = xmlhttp.responseText;
}
ONLINE.PHP
在线PHP
this is the content of my online.php
这是我的 online.php 的内容
<link rel="stylesheet" type="text/css" href="style.css" />
<?php
session_start();
include 'db.inc.php';
class WhosOnline{
$rn = $_GET['room'];
protected $get_status_query = "SELECT * FROM `online_users` WHERE `room` = '{$rn}'";
public function DisplayUsers(){
$get_current_status = mysql_query( $this->get_status_query );
if( mysql_num_rows( $get_current_status ) != 0 ) {
while( $row_status = mysql_fetch_array( $get_current_status ) ) {
if( $_SESSION['username'] == true ) {
echo "<div class='online_margin'>
<b>".base64_decode($row_status['username'])."</b>
</div>
<hr style='border: 0; border-top: solid 1px #D8D8D8;margin: 5px 10px 5px 10px;' />";
}
}
}
}
}
$Online = new WhosOnline;
$Online->DisplayUsers();
?>
Any help?
有什么帮助吗?
采纳答案by craigmj
Ok, even this gives an error:
好的,即使这样也会出错:
class WhosOnline{
public $rn = $_GET['room'];
}
This also gives an error:
这也给出了一个错误:
$v = "Hi there";
class WhosOnline{
public $rn = $v;
}
The error is because you're trying to set a variable based on another variable in the class definition. You could do this in the constructor. Or you can set class members based on CONSTANTS (as you were doing with the query string). But why not rewrite your WhosOnline method like this:
错误是因为您试图根据类定义中的另一个变量来设置变量。您可以在构造函数中执行此操作。或者您可以根据 CONSTANTS 设置类成员(就像您对查询字符串所做的那样)。但是为什么不像这样重写你的 WhosOnline 方法:
public function DisplayUsers(){
$get_current_status = mysql_query(
"SELECT * FROM `online_users` WHERE `room` = '"
. mysql_real_escape_string($_GET['room']) . "'");
if(mysql_num_rows($get_current_status)!=0){
while($row_status = mysql_fetch_array($get_current_status)){
if($_SESSION['username']==true){
echo "<div class='online_margin'> <b>".base64_decode($row_status['username'])."</b></div><hr style='border: 0; border-top: solid 1px #D8D8D8;margin: 5px 10px 5px 10px;' />";
}
}
}
}
This will also remove any potential errors you might have with $this->references missing.
这也将消除您可能因$this->缺少参考而产生的任何潜在错误。
回答by Ignacio Vazquez-Abrams
$rn = $_GET['room']; protected $get_status_query = "SELECT * FROM `online_users` WHERE `room` = '{$rn}'";
$rn = $_GET['room']; protected $get_status_query = "SELECT * FROM `online_users` WHERE `room` = '{$rn}'";
This is a bad habit that you need to break RIGHT NOW.
这是一个坏习惯,你需要打破现在。
protected function get_status_query($rn) {
return "SELECT * FROM `online_users` WHERE `room` = '". sanitize($rn) . "'";
};
Implementation of sanitize()is left to the reader.
的实现sanitize()留给读者。
回答by Moyed Ansari
you could not initialized any variable directly in class , try this
你不能直接在类中初始化任何变量,试试这个
public $rn;
protected $get_status_query;
public __construct(){
$this->rn = $_GET['room'];
$this->get_status_query = "SELECT * FROM `online_users` WHERE `room` = '{$this->rn}'";
}

