dongliming2416 2013-09-23 14:57
浏览 110
已采纳

在嵌套函数中使用$ this错误

I try to use $this in a nested function in my class.

I call the method with:

$check_membership = $this->setAuthorisation($posted_username, $ldap_connection);

the method looks like:

private function setAuthorisation($posted_username, $ldap_connection)
{
    // split the posted username for filtering common name
    $temp_var = explode('\\', $posted_username);
    $cn_name = end($temp_var);

    // filter parameter for ldap_search
    $filter = "objectClass=*";

    // search attribute to get only member
    $attr = array("member");

    // possible membership status:
    // group_membership: "null": No access to enter the page.
    // group_membership:    "1": Access to the page, but no admin rights.
    // group_membership:    "2": Access to the page with admin rights.

    /**
     * perform the setMembershipUser for authorisation the "user" group
     */
    function setMembershipUser($ldap_connection, $cn_name, $filter, $attr)
    {
        // search for user in the authorized ad group "user"
        $user_result = ldap_search($ldap_connection, GROUP_USER.",".BASE_DS, $filter, $attr);

        // reads multiple entries from the given result
        $user_entries = ldap_get_entries($ldap_connection, $user_result);

        // check if cn_name is in $user_entries
        if (preg_grep("/CN=".$cn_name."/i", $user_entries[0]["member"]))
        {
            $this->group_membership = 1;
        } 
        else 
        {
            $this->group_membership = null;
        }
    }
    setMembershipUser($ldap_connection, $cn_name, $filter, $attr);
    return $this->group_membership;
}

in the function setMembershipUser I got the Error "Fatal error: Using $this when not in object context in..."

Can I use $this in nested functions? The outer function is in my class.

  • 写回答

1条回答 默认 最新

  • drasebt1835 2013-09-23 15:03
    关注

    Your nested function is just that... a function. It's not a method of the parent class, even though it exists only within that method. You could pass in the outer $this as a parameter, e.g.

    class foo {
       function bar() {
           function baz($qux) {
              ...
           }
           baz($this);
       }
    }
    

    But... You shouldn't be nesting functions like that anyways. Why not just promote your nested function to a full-blown "regular" function, meaning it would be a method of your class, and then $this would be available as expected.

    As another note, you could not use $global to make $this visible inside the method, because global only looks at the real global scope, it doesn't look in "parent" scopes at all. e.g.

    $x = 42;
    class foo {
       function bar() {
           $x = 13;
           function baz() {
                $x = 69;
                echo $x; // outputs 69
                global $x;
                echo $x; // outputs 42
           }
       }
    }
    

    There is no way for the baz() function to get at $x = 13, because the only scopes available anywhere in PHP are the "local" scope, which is the 69 is defined, and the global scope, where $x is 42.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决