php解析错误:语法错误,意外T_STRING,期待构造上的T_FUNCTION

Ahm*_*nas 1 php syntax-error

嘿伙计们希望你能帮助我...

只是为了让你事先知道,我是一个相对较新的php编码器,做一个练习项目,并遇到了这个问题,我花了一个小时的重新检查和谷歌搜索,但只是无法弄清楚是什么导致它

错误:解析错误:语法错误,意外的T_STRING,在第7行的C:\ wamp\www\forum\classes\ClassUser.php中期待T_FUNCTION

导致问题的代码段:

include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
Class User{


__construct($u,$p){ //this is line 7

    $user=$u;
    if(strlen($p)>30|| empty($p) || !preg_match('/[^a-zA-Z0-9]/i',$p)){
    $password=0;

    }
    else{
    $password=hash_hmac('md5',$p,KEY);

    }

}
Run Code Online (Sandbox Code Playgroud)

哦,因为我是新的PHP,我想做我不应该做的事情,请推荐..在此先感谢.

注意:香港专业教育学院删除了PHP标签,因为他们似乎搞乱了这篇文章的格式:/

note2:我还得到另一个通知注意:使用未定义的常量KEY - 在第20行的C:\ wamp\www\forum\classes\general.inc中假定为'KEY'

但我认为这更像是一个警告而不是一个错误...但只是添加incase它与错误有关

general.inc:

//error definations
define("ERROR_FIELD_EMPTY","Error! All required fields not filled");
define("ERROR_INVALID_SIGNIN","Error! Username/password do not match!");
define("ERROR_GENERAL_INPUT", "Error! Invalid input given");
define("ERROR_SQL_CONNECT","Error! Could not connect to sql database");


//field sizes
define("PASSWORD_LENGTH",12);
define("USERNAME_LENGTH",30);

//sql server details
define("SQL_SERVER_NAME","localhost");
define("SQL_SERVER_USERNAME","root");
define("SQL_SERVER_PASSWORD","");
define("SQL_SERVER_DATABASE","forums");

define(KEY,"key");


function __autoload($className){
    require_once($_SERVER["DOCUMENT_ROOT"]."forum/classes/Class$className.php");

}
Run Code Online (Sandbox Code Playgroud)

ClassUser.php

  include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
    Class User{


    __construct($u,$p){

        $user=$u;
        if(strlen($p)>30|| empty($p) || !preg_match('/[^a-zA-Z0-9]/i',$p)){
        $password=0;

        }
        else{
        $password=hash_hmac('md5',$p,KEY);

        }

    }

    public function validate(){
        if(strlen($user)>30|| empty($user) || preg_match('/[^a-zA-Z0-9]/i',$password==0 )){
        throw new Exception(ERROR_GENERAL_INPUT);



        }
        $user=mysql_real_escape_string($user);
        return true;

    }

    public function insert(){
       // this->validate();

        $conn= mysqli_connect(SQL_SERVER_NAME,SQL_SERVER_USERNAME,SQL_SERVER_PASSWORD,SQL_SERVER_DATABASE);


        if(empty($conn)){
        throw new Exception(ERROR_SQL_CONNECT);
        }

        $query="INSERT into USERS VALUES ($user,$password)";
        $conn->query($query);



    }

    private $user;
    private $password;

    };
Run Code Online (Sandbox Code Playgroud)

NewUser.php

 include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";




    try{
    $user = new User($_POST['UserName'],$_POST['Password']);
    $user->insert();
    }

    catch(Exception $Error){
    echo $Error->getMessage();


    }
Run Code Online (Sandbox Code Playgroud)

Spu*_*ley 6

__construct需要function在它面前public function(validate或者insert在你的情况下)以与其他方法相同的方式在它前面或更好地说出这个词.

即你需要以下内容:

public function __construct($u,$p){ //this is line 7
Run Code Online (Sandbox Code Playgroud)