php如果在课堂外调用功能
我正在为网络平台制作SDK,有一个客户端类需要cookie来授权,并且有一个从cookie获取身份验证令牌的函数。所以我的问题是:如何检查函数是否在类外被调用。我需要这个,因为我想用密码保护这个函数,并且如果类调用它,它就可以在没有密码的情况下工作。 这是我的代码:
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";
}
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";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来很像一个坏主意。
为什么不创建两个函数:一个需要密码的公共函数,一个不需要密码的私有函数。当然,公共函数可以在密码验证后调用私有函数。
像这样的事情:
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: