0

What is the difference between accessing the class through "use" keyword or declaring it as "new" plus the filepath of the class?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

//if accessed using use keyword
use App\Entity\User;

class SampleController extends Controller 
{
    public function add(Request $request) 
    {
       //if declared by "use" keyword above
       $user = new User();

       //if not declared by "use" keyword

       $user = new \App\Entity\User();
    }
}

If I'm going to use the functions of User class, the results are the same but what's the difference in their declarations?

G. Curs
  • 181
  • 1
  • 12
  • 1
    try removing use.. you'll quickly see an error. the `use` keyword kinda is like a require statement in that, it calls the class but doesn't init the class until called, however, `use` isn't necessary - you can call the class by doing `new App\Entity\User\User` but imagine doing that each time you want the user class. That's where `use` comes in to make shorthands for `new` and `new` just inits a class – treyBake Jun 22 '17 at 09:39
  • 1
    `use` is simply setting an alias for the namespaced class definition; `new` instantiates an instance of a class.. very different (and fundamental) things – Mark Baker Jun 22 '17 at 09:40
  • `use` will basically allow you to define 2 classes with the same name, as long as they are declared in 2 different namespaces. In some cases, you will need to specify the namespace before the class name to be sure to use the good one – Kaddath Jun 22 '17 at 09:45
  • use is to tell which namespace you want to use. new is to instantiate the class. The value you give after use keyword is not actually a path. – Nimeshka Srimal Jun 22 '17 at 09:46

2 Answers2

2

There is no difference. By using use doesn't include anything. It just imports the specified namespace (or class) to the current scope

new \App\Entity\User(); is same as new User();

You can find more details How does the keyword "use" work in PHP and can I import classes with it?

shivanshu patel
  • 782
  • 10
  • 19
2

The use keyword produces an alias for a class or a namespace.

The as keyword introduces the alias. Without as, the alias is the last component of the namespace or class path:

use App\Entity\User as OneUser;

OneUser is the same thing as \App\Entity\User and can be used instead of it everywhere in the current file.

use App\Entity\User;

Here the alias is User (the last component of App\Entity\User). It is the same as (but shorter than):

use App\Entity\User as User;

Aliased are used to write less; the code is easier to read this way.

The aliases are processed at compile time and they are visible only in the file where they are created. The mere presence of the use statement does not have any effect; it only creates a shorter name for a class or namespace but this shorter name is valid only during the compilation of the file that contains it.

The aliased class names are not expanded inside strings. During the compile time they are just text. During the runtime, 'User' is not the same as 'App\Entity\User'.

Accordingly, class_exists('User') returns FALSE but class_exists('App\Entity\User') returns TRUE.


For more insights about how PHP resolves the aliases, read the "Using namespaces: Basics" documentation page.

axiac
  • 68,258
  • 9
  • 99
  • 134