7

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...

Liz
  • 189
  • 3
  • 10
  • So called "global" variables in PHP aren't as global as in C or Javascript. You still need to invite them into a functions local scope. See http://www.php.net/manual/en/language.variables.scope.php – mario Dec 07 '13 at 13:44
  • If i don't pass the $f3 class, the page gives an error (Call to a member function get() on a non-object) – Liz Dec 07 '13 at 13:48
  • possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – deceze Dec 07 '13 at 13:54
  • That's not an issue of F3, it's pure PHP here. F3 has its own "global" vars, but they're only available inside of F3. That's why you have to pass the F3 object to your function. Instead of passing it, you could also use `$f3 = Base::instance();` inside of your function, to get the object. – sascha Dec 07 '13 at 14:35

1 Answers1

15

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
    }
}
xfra35
  • 3,833
  • 20
  • 23