在数组php中调用静态函数

时间:2017-02-20 00:32:04

标签: php arrays class web methods

如何在数组中调用静态函数?我尝试了但它给了我一个Constant expression contains invalid operations我有两个类Login类和Route类。

Route类在数组$ routes中使用Login :: getJSFile()但是给出了错误

这是我的代码:

class Route
{
    private static $routes = [
        "login"                     => [
            "add_to_menu"            => false,
            "class"                  => "login-page",
            "component"              => [],
            "css"                    => ["/css/form", "/css/login"],
            "default"                => true,
            "display_footer_menu"    => true,
            "display_header_menu"    => false,
            "enable_authentication"  => true,
            "enable_permission"      => false,
            "form"                   => true,
            "icon"                   => "<i class='fa fa-sign-in fa-fw'></i>",
            "js"                     => Login::getJSFile(),
            "layout"                 => "pages/login.php",
            "list"                   => false,
            "menu_order"             => null,
            "menu_type"              => null,
            "name"                   => "Login",
            "no_layout"              => "pages/no-layout.php",
            "parent"                 => "",
            "php"                    => ["/php/login"],
            "require_authentication" => false,
            "require_permission"     => false,
            "url"                    => "/login/"
        ]
    ];
}


class Login
{
    private static $login_routes = [
        "administrator" => [
            "default"   => false,
            "id"        => "administrator",
            "js"        => [],
            "layout"    => "pages/components/login/administrator.php",
            "name"      => "Administrator",
            "no_layout" => "pages/no-layout.php",
            "url"       => "administrator"
        ],
        "student"       => [
            "default"   => true,
            "id"        => "student",
            "js"        => ["/lib/Inputmask-3.x/dist/min/jquery.inputmask.bundle.min", "/js/components/login/student"],
            "layout"    => "pages/components/login/student.php",
            "name"      => "Student",
            "no_layout" => "pages/no-layout.php",
            "url"       => "student"
        ]
    ];

    public static function getJSFile()
    {
        echo self::$login_routes[self::getCurrent()]["js"];
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道你打算如何使用它,但你可以利用变量:

$login = ${$a['login']['js']};

以下示例/ POC:

class Login {

    function test() { echo 'DERP TEST DERP';}
}

$a = [
    'login' => [
        'js' => Login::test()    
    ]
];

$login = ${$a['login']['js']};

这不是最好的方式,但它实现了所需要的。我 HIGHLY 建议您查看代码库中的备选方案以正确处理此问题。

Example/Demo