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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10872097/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:16:05  来源:igfitidea点击:

php Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION on construct

phpsyntax-error

提问by Ahmed-Anas

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,在第 7 行的 C:\wamp\www\forum\classes\ClassUser.php 中需要 T_FUNCTION

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 新手,所以我做了一些我不应该做的事情,请推荐.. 提前致谢。

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:我还收到另一条通知注意:使用未定义的常量KEY - 在第20行的C:\wamp\www\forum\classes\general.inc中假设为'KEY'

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

但我假设那更多的是警告而不是错误......但只是添加柜面它与错误有关

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

类用户.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

新用户.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();


    }

回答by Spudley

Your __constructneeds to have the word functionin front of it, or better yet public function, in the same way as the other methods in the class (ie validateand insertin your case).

__construct需要有这个词function在它前面,或者更好public function,以同样的方式在类(即其他的方法validateinsert你的情况)。

ie you need the following:

即您需要以下内容:

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

回答by Oscar Broman

Change the line to:

将该行更改为:

public function __construct($u, $p) {