Method not recognized within class

1

I am creating some classes in php and I have the following doubt: for methods get and set in php , I mounted 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 $user->setPassword(1234) of error função encript() não definida , where is the error? I do not know much about php but I've already done it that way in other languages and it usually works, what's the problem?

    
asked by anonymous 27.06.2016 / 17:11

1 answer

2

In php a function is different from a method. When invoking functions just call the name. With methods it is necessary to say who owns it, outside the class is the object, within the class we use $this .

Change:

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

To:

 $this->password = $this->encript($password);
    
27.06.2016 / 17:14