v3 needs $f3 passed into function() as argument?

169 views
Skip to first unread message

stu warneski

unread,
Feb 2, 2013, 11:37:31 PM2/2/13
to f3-fra...@googlegroups.com
i just jumped into the docs tonight to learn about v3, and i see this code:

$f3->route('GET /brew/@count',
    function($f3) {
        echo $f3->get('PARAMS.count').' bottles of beer on the wall.';
    }
);

why do you need to pass the $f3 object into the function as an argument?

it doesn't seem like that was required in v2
F3::route('GET /brew/@count','drink');
	function drink() {
		echo F3::get('PARAMS["count"]').' bottles of beer on the wall.';
	}

is it because in v3 the $f3 var is invoked as an instance, and when calling an anon function you need to pass variables & objects into function so the function has access to that variable & object from it's internal scope?

what if inside the function you tried to use f3::get() instead of $f3->get()
does a call to the static method still work, or is this the new way to write functions in v3?

so all my v2 code will need to be re-written in this way?





stu warneski

unread,
Feb 2, 2013, 11:42:58 PM2/2/13
to f3-fra...@googlegroups.com
i just came across this in a recent thread:

bcosca wrote: 
The F3 class and the corresponding F3:: way of calling methods will be deprecated in the next major release.


so that answers my later question, but i would understand better if someone could explain the need to pass the f3 instance into the function as an argument...

and if the "tickled pink" release is coming out soon, will there be even more changes / updates, so i should hang tight until it is released, instead of starting to upgrade my code?

bcosca

unread,
Feb 3, 2013, 5:15:02 AM2/3/13
to f3-fra...@googlegroups.com
3.0 passes the framework instance and the token values (from the routing pattern) automatically to all route handlers/controllers for the singular purpose of eliminating the need for the following:

$f3->route('GET /beer/@count',function() {
    $f3copy=Base::instance();
    echo $f3copy->get('PARAMS["count"]').' bottles of beer on the wall.';
});

Of course, there's the programmatic alternative:

$f3->route('GET /beer/@count',function() use($f3) {
    echo $f3->get('PARAMS["count"]').' bottles of beer on the wall.';
});

And the old way of doing it (for deprecation):

f3::route('GET /beer/@count',function() {
    echo f3::get('PARAMS["count"]').' bottles of beer on the wall.';
});

Finally. the recommended approach:

$f3->route('GET /beer/@count',function($f3,$args) {
    echo $args['count'].' bottles of beer on the wall.';
});

3.0.4 was released a few days ago. Time to upgrade your code. Wasting CPU cycles on old code isn't the green way of writing apps ;)

stu warneski

unread,
Feb 3, 2013, 3:21:32 PM2/3/13
to f3-fra...@googlegroups.com
thanks. 
just to clarify so I understand,
 
PARAMS is identical to $args in the data that it holds, i.e. $args is the global variable mentioned above, " the token values (from the routing pattern) "

but if you want to access PARAMS you need to use the get method 
$f3->get('PARAMS.count')

so the better way is to use the global variable $args, because it is already available in memory,
so you simply pass it into the function as an argument and it's available to use?

i am almost finished with the clients apps written in v2, so i can't delay my deliverable schedule to upgrade to v3 yet, but in a few weeks i can try to get them to pay me some more money to upgrade the code to v3, and be able to make a donation to f3 :)
and the wasted cpu cycles are the only thing keeping me warm this winter :)







bcosca

unread,
Feb 3, 2013, 8:23:01 PM2/3/13
to f3-fra...@googlegroups.com
PARAMS is identical to $args in the data that it holds, i.e. $args is the global variable mentioned above, " the token values (from the routing pattern) "

Correct.


but if you want to access PARAMS you need to use the get method
$f3->get('PARAMS.count')

Voila! $f3 is already being passed automatically to the receiving function/method.


so the better way is to use the global variable $args, because it is already available in memory,
so you simply pass it into the function as an argument and it's available to use?

Just to be precise, you simply create two variables as receivers. These are optional. F3 passes the framework instance and the token arguments when run() is executed.


i am almost finished with the clients apps written in v2, so i can't delay my deliverable schedule to upgrade to v3 yet, but in a few weeks i can try to get them to pay me some more money to upgrade the code to v3, and be able to make a donation to f3 :)

Upgrading is always a good reason for staying employed and keeping your customers ;)

and the wasted cpu cycles are the only thing keeping me warm this winter :)

Nice to know you haven't thrown the old laptops into the fire yet.








Reply all
Reply to author
Forward
0 new messages