Jump to content

Unexpected Keyword 'new' Error


phreak3r

Recommended Posts

I am having an error here with my code/script. I try and call sanitizeData($data) from functions.php class in createUser.php class. I end up with an expected 'new' T_NEW error.

createUser.php file

<?php
require('dbcon/dbcon.php');
include('fileUpload.php');
include('functions.php');

class createUser {

    public $functionsClassInstance = new helperFunctions();

    public $avatar;
    public $bio;
    public $video_count;
    public $c_status;
    public $usernameI;
    public $username;
    public $password;
    public $email;
    public $doc;
    public $last_logged_in;

       public function addUser(PDO $pdo) {
	// add user info to db
	       $avatar = "/soapbox/assets/soap.jpg";
	       $usernameI = $_POST['username'];
               $username = $functionsClassInstance->sanitizeData($usernameI);
	    //$username = strip_tags(trim($_POST['username'];))
            $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
            $bio = $_POST['bio'];
            $email = $_POST['email'];
	    $c_status = 0;
            date_default_timezone_set('UTC');
	    $doc = date("Y-m-d h:i:s");
	    // account date last seen/logged in
	    // add account age

	    $query = $pdo->prepare("INSERT into profiles001 (username, password, email, c_status, bio, doc, avatar) VALUES (:username, :password, :email, :cstat, :bio, :doc, :avatar)");

	    $query->bindValue(':username', $username);
	    $query->bindValue(':password', $password);
	    $query->bindValue(':email', $email);
	    $query->bindValue(':doc', $doc);
	    $query->bindValue(':bio', $bio);
	    $query->bindValue(':cstat', $c_status);
	    $query->bindValue(':avatar', $avatar);

	    // if user uploads file, add path and file to database and server, if not revert to default.
            if ($_FILES["avatar"]["error"] == 4) {
	        $query->execute();
	    } elseif ($_FILES["avatar"]["error"] != 4) {
	        $file = new fileUpload();
		$file->processFile();
		$avatar = "/soapbox/uploads/" . $_FILES["avatar"]["name"];
	        $query = $pdo->prepare("INSERT into profiles001 (username, password, email, c_status, bio, doc, avatar) VALUES (:username, :password, :email, :cstat, :bio, :doc, :avatar)");
		
		$query->bindValue(':username', $username);
	        $query->bindValue(':password', $password);
	        $query->bindValue(':email', $email);
	        $query->bindValue(':doc', $doc);
	        $query->bindValue(':bio', $bio);
	        $query->bindValue(':cstat', $c_status);
	        $query->bindValue(':avatar', $avatar);
	        $query->execute();
	    }
	    // create variables
	    // initialize variables
	    // bind values of variables being entered into database
       }

}

// this file is responsible for creating the users
?>

functions.php class

<?php
// Functions are stored here
// Any code that is repeated more than once is put into a function to make my life easier
// The start of going from procedural to OOP

// checks if user is logged in or not, limits access to certain pages in/on site.
class helperFunctions {
    function sanitizeData($data) {
	    strip_tags($data);
	    trim($data);
	    return $data;
    }
}
?>

 

 

Link to comment
Share on other sites

There is much that could be said about this code, but to keep on point, you need to use Dependency Injection and pass an instance of the helper class to your user class just like you did with the PDO connection.

* The duplicate query should be one of the first clues of a problem with this class.

 

Quote

// The start of going from procedural to OOP

This pretty much explains what I see here so the current state of the code makes sense for now. You should eventually end up with quite a bit of refactoring when you get it dialed in.

Edited by benanamen
Link to comment
Share on other sites

Do I only have to use Dependency Injection and pass in the instance of the helper class when the function I want to call contains a parameter? I did something similar, but the other function did not contain a parameter. Yet, the call to the function without the parameter went through and the function with the parameter gives me an error. Just trying to make sense of this.

Edited by phreak3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.