为了两个目的使用相同的功能 [英] using same function for two purposes
问题描述
我有一个名为 isLoggedIn()
的函数 - 这会返回一个 FALSE
或 TRUE
根据用户是否登录。它通过检查用户数据是否在session中设置来实现这一点。
我有另一个叫做 getUserSession()
它会在会话中提取并返回用户数据。我的问题是:两者合并可以吗?
我的意思是isLoggedIn()已经访问会话值,所以如果我在函数中返回该值,那么我像这样使用它:
//当我需要会话数据时
$ user = isLoggedIn();
echoHello。$ user ['name'];
//当我需要检查用户是否未登录
if(!isLoggedIn())
{
echo请登录继续!
}
这种方法有问题吗?我的同事说我不应该将它们合并,但我不明白这里有什么问题......
($ user = isLoggedIn()){
echo
Hello。$ user ['name'];
} else {
echo请登录继续!;
}
I have a function called isLoggedIn()
- This returns a FALSE
or TRUE
based on whether the user is logged in or not. It does this by checking if the user data is set in session.
I have another function called getUserSession()
which fetches and returns the user data in session. My question is: Is it ok to merge them both?
I mean isLoggedIn() is already accessing the session values, so if I return that in the function, then I use it like so:
//when I need session data
$user = isLoggedIn();
echo "Hello ".$user['name'];
//when I need to check if user is not logged in
if(!isLoggedIn())
{
echo "Please login to proceed!";
}
Is there a problem with this approach? My colleague says I should not merge them, but I do not see what the concern is here...
If your going to merge them do something like this
if($user = isLoggedIn())
{
echo "Hello ".$user['name'];
} else {
echo "Please login to proceed!";
}
这篇关于为了两个目的使用相同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!