Fat-Free-Framework 全局变量和函数

我是无脂肪框架的新手,我对全局变量有点困惑.

$f3->route('GET/@page','display');功能显示($f3){echo '我不能反对一个对象'.$f3->get('PARAMS.page');};$f3->run();

<块引用>

这里我使用 GET/@page 作为 url 路由的标记.在里面函数我然后使用 $f3->get('PARAMS.page') 来获取该值变量.

既然$f3->get是获取全局变量的方法,那为什么我有将 $f3 类传递给函数.

下面的代码不起作用($f3 类没有传递给函数).

$f3->route('GET/@page','display');功能显示(){echo '我不能反对一个对象'.$f3->get('PARAMS.page');};$f3->run();

所以我的问题是:为什么我必须将 $f3 类传递给函数?

谢谢...

解决方案

在 index.php ($f3=require...) 开头声明的 F3 实例变量可以使用静态调用 $f3=Base::instance() 在代码中的任何位置检索.

无论如何,为了方便起见,在路由时,这个 F3 实例以及路由参数被传递给路由处理程序.因此,不要将您的路由处理程序定义为:

function display() {$f3=Base::instance();echo '我不能反对一个对象'.$f3->get('PARAMS.page');};

您可以将其定义为:

函数显示($f3) {echo '我不能反对一个对象'.$f3->get('PARAMS.page');};

甚至更好:

函数显示($f3,$params) {echo '我不能反对一个对象'.$params['page'];};

这 3 个功能完全相同,因此您应该选择最了解的一个.但是你应该记住 $f3$params 只在路由时间传递,这意味着 3 个函数:路由处理程序,beforeRoute() 钩子和 afterRoute() 钩子.在代码中的任何其他地方(包括在类构造函数中),您应该调用 Base::instance() 来检索 F3 实例.

PS:你的问题是为什么我必须将 $f3 类传递给函数?",我建议你重命名它的标题以反映它.

更新: 从 3.2.1 版本开始,F3 实例也被传递给路由处理程序类的构造函数:

class myClass {功能显示($f3,$params){echo '我不能反对一个对象'.$params['page'];}函数 __construct($f3) {//用 $f3 做一些事情}}

I'm new to fat free framework and i'm a little bit confused about the global variables.

$f3->route('GET /@page','display');

    function display($f3) {
        echo 'I cannot object to an object' . $f3->get('PARAMS.page');
    };

$f3->run();

Here i'm using GET /@page as a token for the url route. In the function i then use $f3->get('PARAMS.page') to get the value of that variable.

Since $f3->get is the method to get a global variable, why do i have to pass the $f3 class to the function.

The below code doesn't work ($f3 class not passed to the function).

$f3->route('GET /@page','display');

    function display() {
        echo 'I cannot object to an object' . $f3->get('PARAMS.page');
    };

$f3->run();

So my question is: why do i have to pass the $f3 class to the function?

Thx...

解决方案

The F3 instance variable which is declared at the very start of your index.php ($f3=require...) can be retrieved anywhere in the code using the static call $f3=Base::instance().

Anyway, for convenience purpose, at routing time this F3 instance as well as the route parameters are passed to the route handler. Therefore, instead of defining your route handler as:

function display() {
    $f3=Base::instance();
    echo 'I cannot object to an object' . $f3->get('PARAMS.page');
};

you could define it as:

function display($f3) {
    echo 'I cannot object to an object' . $f3->get('PARAMS.page');
};

or even better:

function display($f3,$params) {
    echo 'I cannot object to an object' . $params['page'];
};

These 3 functions are absolutely identical so you should pick up the one that you understand best. But you should remember that $f3 and $params are only passed at routing time, which means to 3 functions: the route handler, the beforeRoute() hook and the afterRoute() hook. Anywhere else in the code (including inside a class constructor), you should call Base::instance() to retrieve the F3 instance.

PS: your question being "why do i have to pass the $f3 class to the function?", I would suggest you to rename its title to reflect it.

UPDATE: Since release 3.2.1, the F3 instance is also passed to the constructor of the route handler class:

class myClass {
    function display($f3,$params) {
        echo 'I cannot object to an object' . $params['page'];
    }
    function __construct($f3) {
        //do something with $f3
    }
}

相关文章