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

时间:2021-01-10 22:46:01

hey guys was hoping you could help me out..

嘿,伙计们,希望你能帮我。

just to let u know in advance, im a relatively new php coder, doing a practice project, and came across this problem and ive spent like an hour of rechecking and googling but just cant figure out whats causing it

为了让你提前知道,我是一个比较新的php程序员,做了一个实践项目,遇到了这个问题,我花了一个小时的时间来重新检查和搜索,但是却无法找到导致它的原因。

error: Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\wamp\www\forum\classes\ClassUser.php on line 7

错误:语法错误,意外的T_STRING,期望T_FUNCTION在C:\wamp\www\论坛\ClassUser。php在第7行

the segment of the code causing the problem:

导致问题的代码段:

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);

    }

}

oh and since im new to php, incase im doing something which i should not be, please to recommend.. thanks in advance.

哦,既然我刚到php, incase我正在做一些我不应该做的事情,请推荐…提前谢谢。

note:ive removed the php tags since they seemed to be messing with the formatting of this post :/

注意:我删除了php标签,因为它们似乎在干扰这个帖子的格式:/。

note2: im also getting another notice Notice: Use of undefined constant KEY - assumed 'KEY' in C:\wamp\www\forum\classes\general.inc on line 20

注2:我还收到了另一个通知通知:使用未定义的常数键——在C:\wamp\www\ \ \ \ \ \ \ \ \ \ \ \一般的\ \ \ \ \ \ \ \。公司在20行

but im assuming thats more of a warning than an error... but just adding incase it has something to do with the error

但我认为这与其说是一个错误,不如说是一个警告……但只是添加了incase它与错误有关。

general.inc:

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");

}

ClassUser.php

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;

    };

NewUser.php

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();


    }

2 个解决方案

#1


6  

Your __construct needs to have the word function in front of it, or better yet public function, in the same way as the other methods in the class (ie validate and insert in your case).

您的__construct需要在它前面有word函数,或者更好的公共函数,就像类中的其他方法一样(即验证和插入到您的案例中)。

ie you need the following:

你需要以下内容:

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

#2


1  

Change the line to:

改变线:

public function __construct($u, $p) {

#1


6  

Your __construct needs to have the word function in front of it, or better yet public function, in the same way as the other methods in the class (ie validate and insert in your case).

您的__construct需要在它前面有word函数,或者更好的公共函数,就像类中的其他方法一样(即验证和插入到您的案例中)。

ie you need the following:

你需要以下内容:

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

#2


1  

Change the line to:

改变线:

public function __construct($u, $p) {