0

I am trying to define a constant inside of a function but I keep getting a syntax error. If I define this at the top of the file outside of the function and class it works define('SALT_LENGTH', 9); As soon as I put this inside the class or function I get syntax error I have also tried const SALT_LENGTH = 9; but it is the same issue. I have been searching around for answers but I am not finding anything.

class userFunctions{

//Generate an encrypted password method
    public function generateHash($password, $salt = null)

{
    if ($salt === null) {
        $salt = substr(md5(uniqid(rand(), true)), 0, SALT_LENGTH);
    } else {
        $salt = substr($salt, 0, SALT_LENGTH);
    }
        return $salt . sha1($salt . $password);
}
    //End of generate encrypted password method
}
1
  • define is a function you can't just call it while you're not inside a function scope. Commented Sep 14, 2013 at 2:00

2 Answers 2

1

You got to use the 'self' keyword.

Like this,

class userFunctions{

const SALT_LENGTH = 5; // define constant 


public function generateHash($password, $salt = null)

{

    if ($salt === null) {
        $salt = substr(md5(uniqid(rand(), true)), 0, self::SALT_LENGTH);
    } else {
        $salt = substr($salt, 0, self::SALT_LENGTH);
    }
    return $salt . sha1($salt . $password);
}

}

Sign up to request clarification or add additional context in comments.

Comments

0

Please use constructor in your class. Change any value you prefer for SALT_LENGTH. I chose 50. Below is the code.

 function __construct() {
        define('SALT_LENGTH','500');
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.