আগামী ৩০ অক্টোবর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML,CSS,Bootstrap,PHP,MySQl, AJAX and JQUERY কোর্সের ৮৬ তম ব্যাচ। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722817591, Email : [email protected] কোর্সের সিলেবাস এর জন্য এখানে ক্লিক করুন ।

Variables With Fat-Free Framework

Variables defined in Fat-Free are global. They can be accessed by any MVC component. Framework globals are not identical to PHP globals. Fat-Free variables accept all PHP data types, including objects and anonymous function.

To assign a value to a Fat-Free variable:

$f3->set('var', 'value');

To set several variables at once:

$f3->mset(
	array(
		'foo' => 'bar',
		'baz' => 123
	)
);

To retrieve the value of a framework variable named var :-

$f3->get('var');

To remove a Fat-Free variable from memory if no longer need it:

$f3->clear('var');

To find out if a variable has been previously defined :

$f3->exists('var');

Now we will practice those, first clean up you index.php file if you need. and a new routing like below::

$f3->route('GET /apple',
    function($f3) {
        $f3->set('color', 'Green');
        echo "{$f3->get('color')} apples. <br>";

        $f3->mset(
            array(
                'quality' => "Red",
                'quantity' => 3
            )
        );

        if($f3->exists('quality')) {
            echo "{$f3->get('quantity')} {$f3->get('quality')} apples. <br>";
        }

        $f3->clear('quantity');
        if($f3->exists('quantity')) {
            echo "{$f3->get('quantity')} apples. <br>";
        }
    }
);

output at url:: http://localhost/helloworld/apple is ::

Green apples.
3 Red apples.
Red apples.

In above example we are setting value by “set” and getting value by “get” function.
setting multiple value at once by “mset” function.
checking a given variable is exists or not by “exists” function.
And to removed the variable we used here “clear” function.

List of Fat-Free built-in framework variables:: System Variables

Use of framework variables is recommended, instead of PHP’s, to help you with data transfer across different functions, classes and methods. They also have other advantages:-

1) You can use framework variables directly in your templates.
2) You don’t have to instruct PHP to reference a variable outside the current scope using a global keyword inside each function or method. All F3 variables are global to your application.
3) Setting the Fat-Free equivalent of a PHP global like SESSION also changes PHP’s underlying $_SESSION. Altering the latter also alters the framework counterpart.

As a rule, framework variables do not persist between HTTP requests. Only SESSION and COOKIE (and their elements) which are mapped to PHP’s $_SESSION and $_COOKIE global variables are exempt from the stateless nature of HTTP.

Fat-Free framework variable names must start with an alpha character and should have no space and are case-sensitive.
Variable name may contain any number of letters, digits and underscores. WE can not use PHP reserved words as framework variable names.

Fat-Free has a number of helpful tools to work with framework variables::
Concatenation::

$f3->set('color', 'Green');
$f3->concat('color', ' and Red');
echo $f3->get('color');

output:: Green and Red

Copy::

$f3->copy('color', 'quality');
echo $f3->get('quality');

output:: Green and Red

Array operations::

$f3->set('colors', array('black', 'green', 'red'));
echo $f3->get('colors.1');

output:: green

$f3->push('colors','yellow');
echo $f3->get('colors.3');

output:: yellow

$f3->unshift('colors','purple');
echo $f3->get('colors.0');

output:: purple

echo $f3->shift('colors');

output:: purple

So in this post we get idea about Fat-Free framework variables.

I am a student of Web Application Development courses at Google University.
One comment on “Variables With Fat-Free Framework

Leave a Reply

Your email address will not be published. Required fields are marked *