PHP 魔法常量 - 自动传递给函数?

PHP magic constants - automatically passed to function?

我写了一个简单的访问控制系统,它读取一个访问字符串数组,return true 或 false 取决于结果。

我会这样称呼它(例如在 class User 的方法 list_user_data 中):`

if (current_user_can(__CLASS__, __METHOD__)) { 
    ... 
}

并在那里检查当前用户是否有权访问 class User.

中的方法 list_user_data

它有效,但我觉得很烦人,因为我总是必须在调用中指定 __CLASS____METHOD__。有没有一种方法可以从 函数中获取这些值 调用 函数的 current_user_can 函数,这样我就可以简单地调用 current_user_can() 无需传递魔法常量?

我的代码按原样运行,但我认为它可以改进。

这可能吗?

来自 debug_backtrace 的 return 值应该 return 第二个条目(索引 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();