Welcome Guest, Not a member yet? Register   Sign In
Namespace or function?
#1

Hi,

What do you prefer calling other classes, using the namespace like new \App\Model\ModelName or with the function model('App\Model\ModelName') ?
I haven't used this function before and am wondering what is the best approach to this, for me namespaces have more logic (in my head) but maybe someone has an answer for this?
Reply
#2

Depends on your needs:

1. If you need an always fresh instance, you can use the "new" keyword.
2. If you need the shared instance, you can use the model function.
3. Alternative call for 1, you can also use the model function but passing false as 2nd argument.
Reply
#3

I prefer to use the function in almost every case since I usually want the shared instance. I still tend to call it with the namespaced class name though to make my code easier to read, search, and analyze:

use App\Models\ModelName;

$object = model(ModelName::class)->first();
Reply
#4

Thank you for the clarifycation on why to use the function, makes sense now!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB