Method is not recognized within the class

Asked

Viewed 28 times

1

I’m creating some classes in php and I have the following question: for methods get and set in php, I rode as follows:

class User
{
    private $username = '';
    private $password = '';

    public function setPassword($password){
        $this->password = encript($password);
    }

    public function getPassword(){
       return $this->password;
    }

    function encript($data){
        return sha1($data);
    }
}

but when I call it so $user->setPassword(1234) of error função encript() não definida, where is the mistake? I don’t know much about php but I’ve done it this way in other language and it usually works, what’s the problem?

1 answer

2


In php a function is different from a method. Calling functions just calls the name. With methods it is necessary to say who owns it, outside the class is the object, inside the class is used the $this.

Change:

$this->password = encript($password);

To:

 $this->password = $this->encript($password);
  • weird, but thanks, it worked!

  • @Hebertdelima without the $this PHP thinks you are calling a function encript() outside the class, this abbreviation that you quoted in the question works in other languages like java for example, in PHP the syntax gets a little longer :P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.