Take an example login() function within a class Account.

以类帐户中的示例login()函数为例。

class Account {
 /* Class variables */

    public function login() {
        if(isset($_POST['username']) && isset($_POST['password']))
            return $this->_formLogin();
        else if(isset($_SESSION['accountId']))
            return $this->_sessionLogin();
        else if(isset($_COOKIE['username']) && isset($_COOKIE['password']))
            return $this->_cookieLogin();
        else return false;
    }

    private function _formLogin() {
        //perform login actions using $_POST data
    }
    /* All that other stuff */
}

Try for this moment to ignore any concerns about unseen methods for data sanitizing, password salting, and such. Concentrating strictly on login(), is this global access bad juju? I avoid using PHP super globals within classes normally, but I can't think of a good reason not to do it in this situation.

试着忽略对数据清理,密码腌制等看不见的方法的任何担忧。严格集中在login(),这是全球访问坏juju?我通常避免在类中使用PHP超级全局变量,但我想不出在这种情况下不这样做的好理由。

I can understand why you wouldn't want magic-in-the-background occuring with globals interacting across classes, but these globals are built into PHP, aren't modified by the class, and are only used by this class.

我可以理解为什么你不希望魔术在背景中与全局变量跨类交互,但这些全局变量是内置于PHP中的,不会被类修改,只能由这个类使用。

It would result in this at the beginning of pages you needed a user logged in on:

这将导致您需要用户登录的页面的开头:

$user = new Account($whatever, $objects, $we, $depend, $on);
if($user->login()) {
    //Do this stuff when logged in
}

instead of this on every page, the logic of which may need to be changed later:

而不是在每个页面上,其逻辑可能需要在以后更改:

$user = new Account($whatever, $objects, $we, $depend, $on);
if(isset($_POST['username']) && isset($_POST['password']))
    $user->formLogin($_POST['username'], $_POST['password']);
else if(isset($_SESSION['accountId']))
    $user->sessionLogin($_SESSION['accountId']);
else if(isset($_COOKIE['username']) && isset($_COOKIE['password']))
    $user->cookieLogin($_COOKIE['username'], $_COOKIE['password']);
if($user->isLoggedIn() {
    //Do this stuff when logged in
}

And while I'm aware that creating a function outside of the class to handle that is an option, wouldn't that be just as bad as obfuscating globals in a class?

虽然我知道在类之外创建一个函数来处理它是一个选项,这不会像在类中混淆全局变量一样糟糕吗?

4 个解决方案

#1


I wouldn't say there is a straight yes or no answer to this one. What the idea (with all superglobals $_GET $_POST $_SESSION) is that you are asking for data that sits in your entire application, and not local to the scope you are asking for.

我不会说这个答案是肯定的或没有答案。这个想法(与所有超级全局$ _GET $ _POST $ _SESSION一起)是您要求的数据位于整个应用程序中,而不是您要求的范围本地。

What can happen with these superglobals is that what if they change somewhere for whatever reason just before or (god forbid) during the execution of your function. That can turn out to be a very annoying bug to reproduce.

这些超级全球可能会发生的事情是,如果他们在执行你的功能之前因某种原因而改变某些地方或(上帝保佑)。这可能会成为一个非常烦人的错误重现。

So I would say it is bad form.

所以我会说这是糟糕的形式。

更多相关文章

  1. 在通过AJAX响应发送的页面上执行javascript函数
  2. 使用jQuery和ajax更改浏览器地址栏URL,无需重载页面[重复]
  3. 阅读使用JavaScript生成的页面
  4. PHP教程之PHP调用session_start后页面始终加载的问题研究
  5. jQuery ajax调用不会调用我的php页面
  6. 使用安全的会话-cookie在HTTP和HTTPS页面之间切换
  7. mySQL库编码,PHP页面编码和mysql_query("set names 'GBK'")编码问
  8. 我需要PHP页面显示mysql数据库中的BLOB图像
  9. InnoDB辅助索引页面的物理结构是什么样子的

随机推荐

  1. c语言中的注释符号是什么
  2. c语言的标识符允许使用关键字吗?
  3. c语言函数类型有几种
  4. c语言输出语句是什么
  5. 输入一个字符,如何判断是字母,数字还是特
  6. asp.net是什么?
  7. C语言怎么输入十个数输出最大值
  8. %lf在c语言中表示什么
  9. c程序编译后生成什么文件
  10. 在C语言中,要求参加运算的数必须是整数的