XF 2.2 What is the difference in these find calls?

Brent W

Well-known member
I am rewriting an old add-on as well as adding a few new features to it as a project to get more familiar with xenForo 2.2 code structure, classes and such. I have three different lines of code that seem to all do the exact same thing and work and I am confused on why they all three work and which one I should be using.

The three pieces of code are:

PHP:
$this->app->find()
PHP:
$this->app()->find()
PHP:
$this->app->em()->find()

All three work and all three return the data I am expecting.

1) Why do all three work
2) Why are there three different ways to do the same thing
3) Which should I be using?

PHP:
use XF\Widget\AbstractWidget;

class Test extends AbstractWidget {
    protected function find() {
       
    }
}

The three examples are used inside a method structured similar to above.
 
Last edited:
If you inspect the definitions you will find they all call the same underlying method. The app() method on widget objects simply returns the app property. The find() methods on the app object mostly exist for convenience/brevity, but they simply forward the call to the entity manager. You can use whichever you prefer, depending on how you weigh verbosity against explicitness. I personally tend to prefer local properties where available, and the app methods for conciseness, so $this->app->find(...).
 
If you inspect the definitions you will find they all call the same underlying method. The app() method on widget objects simply returns the app property. The find() methods on the app object mostly exist for convenience/brevity, but they simply forward the call to the entity manager. You can use whichever you prefer, depending on how you weigh verbosity against explicitness. I personally tend to prefer local properties where available, and the app methods for conciseness, so $this->app->find(...).
Thanks Jeremy.
 
Top Bottom