Ahmed-Anas Ahmed-Anas - 1 month ago 8
PHP Question

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

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

error: Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\wamp\www\forum\classes\ClassUser.php on line 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.

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

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

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

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

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




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

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


}

Answer

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

ie you need the following:

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