1

I have two queries regarding the use of "use" keyword in php(laravel).

Below is a code excerpt from laravel docs:

use App\Flight;

$flights = App\Flight::all();

foreach ($flights as $flight) {
    echo $flight->name;
}

The code in line 1 allows us to use Flight model and line 2 has a statement which fetches all the records. So, in line 2 cannot we just say Flight::all(); instead of App\Flight::all();. Are both correct and does it relate to relative and absolute path stuff?

Second Query(related to softDeletes): Below is a code block from one of my models:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $fillable = ['title','body'];
}

Here on line 6, the following statement is used:

use SoftDeletes;

Why are we again using use keyword with softDeletes because we didn't use that with model and simply we could do :

class Post extends Model

without the following code statement:

use Model;

in case of models.

antzshrek
  • 9,276
  • 5
  • 26
  • 43
Brainy Prb
  • 433
  • 1
  • 9
  • 22
  • first one is model and second is trait – Jignesh Solanki Nov 30 '17 at 07:38
  • https://stackoverflow.com/questions/29001191/laravel-5-use-statements this link can help you for better understanding of use statement at the beginning of a file and use statement inside of class file is trait, for that see http://php.net/manual/en/language.oop5.traits.php – Sohel0415 Nov 30 '17 at 07:44

2 Answers2

5

The use command in PHP is used to import/alias namespaces (docs).

First of all, you need to know that this command supports three types of importing/aliasing: classes, interfaces and namespaces.

Your first example is a class import:

use App\Flight;

$flights = Flight::all();

foreach ($flights as $flight) {
    echo $flight->name;
}

It basically imports the class Flight found inside the namespace App, so we assume that the class would start like this:

namespace App;

...

class Flight extends Model {...}

As an alternative you could just import the namespace and reference the class with the namespace it belongs to:

use App;

$flights = App\Flight::all();

foreach ($flights as $flight) {
    echo $flight->name;
}

Or you can avoid importing anything at all and use absolute references:

$flights = \App\Flight::all();

foreach ($flights as $flight) {
    echo $flight->name;
}

In your second example SoftDeletes is a trait, which is similar to a class, but only intended to group functionality in a consistent way (in this case, all the methods needed to perform soft deletes).

The trait looks like this:

namespace Illuminate\Database\Eloquent;

trait SoftDeletes{...}

As you can see it follows the same logic as before: look in the namespace Illuminate\Database\Eloquent and use the class/trait SoftDeletes so I can reference it.

Hope this helps you.

Asur
  • 3,727
  • 1
  • 26
  • 34
2

1 - use used as importing App\Flight class. Following statements both are correct Flight::all() and \App\Flight::all() (The \ in front is important, if you leave the \ in front php will look for namespace from current namespace - relative).

2 - use used in the context of Traits Manual. SoftDeletes is a trait & Model is a class.

Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70