简体   繁体   中英

Laravel 5.2 `use` syntax

What is the difference in usage within classes of the use statement, in the examples below:

Use Sluggable;

Class Invoice extends Model
{

and

Class Invoice extends Model
{
   Use Sluggable;

Have not been able to find answer to this.

The use statement outside the class is for creating a shorter name than the fully namespaced version of a class you plan to use, eg

use \Package\Super\Awesome\Long\Thingy;

at the top of a file will allow you to instantiate the class anywhere in the file by just

new Thingy();

You can also alias classes this way, eg

use \Package\Super\Awesome\Long\Thingy as OtherThingy;

and now you can instantiate this class with

new OtherThingy();

The second example you give is to include a trait in the class you are declaring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM