Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP神奇常量-自动传递给函数?_Php_Magic Constants - Fatal编程技术网

PHP神奇常量-自动传递给函数?

PHP神奇常量-自动传递给函数?,php,magic-constants,Php,Magic Constants,我编写了一个简单的访问控制系统,它读取访问字符串数组,并根据结果返回true或false 我会这样称呼它(例如在类user的方法list\u user\u data中):` 在里面,它检查当前用户是否有权限访问classuser中的方法list\u user\u data 它是有效的,但我发现我总是必须在调用中指定\uuu类\uuuuuu和\uuuu方法\uuuuuu,这让我很恼火。有没有一种方法可以从current_user_can函数中获取调用函数的值,这样我就可以简单地调用current_

我编写了一个简单的访问控制系统,它读取访问字符串数组,并根据结果返回true或false

我会这样称呼它(例如在类
user
的方法
list\u user\u data
中):`

在里面,它检查当前用户是否有权限访问class
user
中的方法
list\u user\u data

它是有效的,但我发现我总是必须在调用中指定
\uuu类\uuuuuu
\uuuu方法\uuuuuu
,这让我很恼火。有没有一种方法可以从
current_user_can
函数中获取调用函数的值,这样我就可以简单地调用
current_user_can()
,而不必传递神奇常数

我的代码按原样工作,但我认为它可以改进

这可能吗?

的返回值应该在第二个条目(索引1)中返回调用函数,例如:

<?php

function current_user_can()
{
    $backtrace = debug_backtrace(false, 2);
    // ToDo: Check if $backtrace[1] (and especially the class-key of that) actually exist...
    //       It should always (although the class key might not if this function isn't called from within a class), since this function is being called
    //       but it's still a good habbit to check array keys before accessing the array
    $callerClass = $backtrace[1]["class"];
    $callerMethod = $backtrace[1]["function"];

    // ToDo: implementation of check, in this example $callerClass would be "User" and $callerMethod would be "list_user_data"

    return true;
}

class User {
    public function list_user_data() {
        if (current_user_can())
        {

        }
    }
}

$user = new User();
$user->list_user_data();

也许值得一看。啊,这很有效,谢谢。请张贴作为我接受的答案。
<?php

function current_user_can()
{
    $backtrace = debug_backtrace(false, 2);
    // ToDo: Check if $backtrace[1] (and especially the class-key of that) actually exist...
    //       It should always (although the class key might not if this function isn't called from within a class), since this function is being called
    //       but it's still a good habbit to check array keys before accessing the array
    $callerClass = $backtrace[1]["class"];
    $callerMethod = $backtrace[1]["function"];

    // ToDo: implementation of check, in this example $callerClass would be "User" and $callerMethod would be "list_user_data"

    return true;
}

class User {
    public function list_user_data() {
        if (current_user_can())
        {

        }
    }
}

$user = new User();
$user->list_user_data();