Home > front end >  Php if function was called outside of class
Php if function was called outside of class

Time:04-10

I'm making SDK for a web platform, there is a client class which requires cookie to authorize and there is a function that gets auth token from cookie. So my question is: how to check if function was called outside of class. I need this because i want to protect this function with password and make it so if class called it, it would work without a password. Here is my code:

public function gettoken(?string password = ""): string{
    //check if it's called inside of class
    if (fromClass() == true){
       //code that gets token
    }
    //if it's called outside of class
    if ($password == $this->password){
       //code that gets token
    }
    return "Incorrect password";
}

CodePudding user response:

This sound very much like a bad idea.

Why not make two functions: One public that requires a password and one private that doesn't. The public function can, of course, call the private function after the password has been verified.

Something like this:

public function getTokenUsingPassword($password)
{
    if ($password == $this->password) {
        return $this->getToken();
    }
    return false;
}

private function getToken()
{
    return //code that gets token
}
  • Related