Fail to understand the different ways that $3 is "passed" to class-methods

69 views
Skip to first unread message

Manne Hoffmann

unread,
Jan 22, 2023, 2:45:41 AM1/22/23
to Fat-Free Framework
Hello there f3-community,
I am still in the process of wrapping my head around OOP in general and the way things work in FatFreeFramnework in particular, so please bear with me if my terminology is not correct ;-)
     
I am trying to understand, how the $f3-object should be "passed" to class-methods, and so far from studying existing code of other f3-based software I have found this:
  • In Controllers the methods that need to use the $f3-object require it as a parameter like so:
    ```php
        public function set_foo(\Base $f3, $bar) {
            // Code here ...
            $f3->set('foo', $bar);
        }
    ```
  • In Models the methods that need to use the $f3-object just "grab" it like so:
     ```php
     public function set_foo($bar) {
             // $f3 is not "passed" when calling the method,
             // but it is now "grabbed" inside the method...
             $f3 = \Base::instance();
             $f3->set('foo, $bar);
          }
    ```

I have tried both approaches in my own classes and both work.
Can anyone try to explain the difference between the to "versions" of doing this?
     
Thanks in advance,
Manfred

ved

unread,
Jan 22, 2023, 8:41:23 AM1/22/23
to Fat-Free Framework
Hi there,

There aren't that many differences and they're mostly two different ways of achieving the same result.
You are not required to use the $f3 instance as a parameter on your controllers and you can also use the same method of just calling "$f3 = \Base::instance()" as well.

Another thing that is also commonly done is to create a base class that your other classes extend, and on that class add a property for the F3 instance. Something like:

class SomeBaseClass
{
    protected $f3;
    public function __construct()
    {
        $this->f3 = \Base::instance();
    }
    // more common or shared properties or methods can also be in this class
}


And then have your other classes extend the SomeBaseClass which will give them access to $this->f3 which will be current the F3 instance.
But this is pretty much exactly the same as just calling $f3 = \Base::instance() whenever you need to access some F3 functionality.
You are also not required to use MVC with F3 so the framework makes it very flexible for developers to organize their classes, folder structure, etc in anyway they like so you have easy access to the F3 instance regardless of methodology.

The reason for this is that $f3 should only be initialized and exist once (singleton) therefore it is derived from another class "Prefab" that is used as a registry for singleton objects
The "instance()" method returns the current instance of a singleton class.

Other classes in F3 that also need to be singletons also inherit this instance() method to get their current instance.
For example:

$cache = \Cache::instance();  // gets the instance of the Cache class
$web = \Web::instance();      // gets the instance of the Web class.

And you can also create your own classes that extend the Prefab class and inherit this behavior (as you can read from the docs link above for the prefab class)

Sorry if my attempt at explaining this wasn't the most clear, I'm not a developer for F3 but have just used it for many years now so I'm pretty sure it's mostly correct but feel free to ask for any clarifications or more details.

Cheers 
Reply all
Reply to author
Forward
0 new messages