doudou0111 2014-02-17 23:45 采纳率: 0%
浏览 157
已采纳

解析错误:语法错误,意外的'$ _POST'(T_VARIABLE)错误,我只是不明白为什么?

I am trying to write this simple class in php as follows:

<?php
session_start();
require 'db_con.php';
class logg{

    private $db;
    private $username = $_POST['usr_name']; 
    private $password = $_POST['usr_pass'];
    private $query = "SELECT * FROM user WHERE nick='$username'";

function __construct(){
    $this->db = new DB();
}

function loggIn(){
    try{
        $res = $this->db->fetch($query)
        foreach($res as $row) {
            $dbusername = $row['nick'];
            $dbpassword = $row['pass']; }
        if ($username == $dbusername && $password == $dbpassword) {
            $_SESSION['username'] = $dbusername;
            header('Location: index.php');
        } catch (Exception $e){
        echo'Wrong login information!'
        }
    }
}   

function loggOut(){
    session_destroy();
}
}

?>

but i keep getting the following error: Parse error: syntax error, unexpected '$_POST' (T_VARIABLE) and i just cant figure out why! Even if i remove theese rows:

    private $username = $_POST['usr_name']; 
    private $password = $_POST['usr_pass'];

i instead get the following error:

    ( ! ) Parse error: syntax error, unexpected '"' in 

im certain the error is a real simple one, but I just can't seem to figure it out..! Any help appriciated!

  • 写回答

2条回答 默认 最新

  • dousui7410 2014-02-17 23:59
    关注

    Aside from a syntax error on your echo line, the actual problem resulting in this particular error is that you can only initialise class members like that with constants.

    Since constants never begin with a $, your initialisations exhibit a syntax error.

    You'll have to assign to those members in the class's constructor, instead, just like you do for $db.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧