4

I used use the keyword "use" generally above the class definition. Like this:

<?php
namespace suites\plugins\content\agpaypal;
use \Codeception\Util\Fixtures;
use \Codeception\Verify;
use \Codeception\Specify;

class agpaypalTest extends \Codeception\Test\Unit
{
    protected $tester;
    ...

But now I realised, that I have to put the line for the trait Specify into the class definition. Like this:

 <?php
namespace suites\plugins\content\agpaypal;
use \Codeception\Util\Fixtures;
use \Codeception\Verify;

class agpaypalTest extends \Codeception\Test\Unit
{
    use \Codeception\Specify;

    protected $tester;
    ...

I think it is because the package \Codeception\Specify; is a trait. But I do not understand why I couldn't reuse this trait when I set the line use \Codeception\Specify; before the class definition?

I would be happy if someone could point me to a hint or an explanaiton that explains to me where I should use the keyword "use" the best.

mega6382
  • 9,211
  • 17
  • 48
  • 69
astridx
  • 6,581
  • 4
  • 17
  • 35

3 Answers3

11

In PHP, the keyword use is used in 3 cases:

  1. As class name alias - simply declares short name for a class (must be declared outside of the class definition) (manual: Using namespaces: Aliasing/Importing )
  2. To add a trait to a class (must be declared inside (at the top) of the class definition) (manual: Traits)
  3. In anonymous function definition to pass variables inside the function (manual: Anonymous functions)
pumbo
  • 3,646
  • 2
  • 25
  • 27
3

You can not import class with use keyword. You have to use include/require statement. Even if you use some php auto loader, still autoloader will have to use either include or require internally.

The Purpose of use keyword:

Consider a case where you have two classes with same name; you'll find it strange, but when you are working with big MVC structure, this happens. So if you have two classes with same name, put them in different name spaces. Now consider when your auto loader is loading both classes (does by require), and you are about to use object of class. In this case, the compiler will get confused which class object to load among two. To help the compiler make a decision, you can use the use statement so that it can make a decision which one is going to be used on.

Here refer this How does the keyword 'use' work

Community
  • 1
  • 1
Sylvester
  • 388
  • 4
  • 12
  • Thank you very much for your answer. I realised, that "use" doesn't include anything. It just imports the specified namespace to the current scope. But in the case of use \Codeception\Specify; it does only work, when I put this line into the class definition (like in example 2). If I put the line above (like in example 1), the namespace is not imported. I think it is because Specify is a trait and no class. But I am not sure ... – astridx Mar 31 '17 at 08:58
2

use is basically including a class in the file to use it. There are two ways to include a class file in another file. The most general is require or include method. Another method is using composer. Assume this Directory Structure

Project
  |
  |--- Folder A
  |      |
  |      |---UserRegistration.php
  |
  |---Example
         |
         |--TestUserRegistration.php    

In Folder A there is UserRegistartion.php and you want to use the code in TestUserRegistration.php In UserRegistration.php It can be class, trait or Interface

Method 1.

In TestUserRegisteration.php you can include or require file UserRegistartion.php
and use it

Method 2

Using Composer. In UserRegistration.php you define namespace FolderA; as the first line of code. Then write your code as you do. So Now you want to use this file in TestUserRegistration.php you do

include vendor/autoload.php;
use FolderA\UserRegistration;

Which one is better and why?

Method 2 using composer is the best method. In method 1 wherever you want to include UserRegistration you have to find the relative path to UserRegistration file. So lets assume some day you need to change the directory structure your application will break as the relative path you had provided now it does'nt exist.

But in Method 2 you always use the namespace you provided \ The filename instead of where you want to use. So even you change the directory structure you don't have to got all codes and modify the path. It will work as it was. To know more study about how to use namespace and composer.

M A SIDDIQUI
  • 2,028
  • 1
  • 19
  • 24
  • thank you, finally makes sense. Method 1 may actually be better when you're dealing with functions. For example function func_name() { include vendor/autoload.php; use FolderA\UserRegistration;} will fail. At least now I know how to fix this. – Robert Sinclair Jul 28 '17 at 18:46
  • 2
    This is wrong and misleading. `use` simply defines an alias for a class. It does not include the file/class, nor does it tell autoloader to do so. – pumbo Oct 19 '17 at 11:42
  • 1
    Given this is a false statement about use, this should not have been upvoted at all. Thanks AlexM for the warning. – Lizardx Nov 19 '17 at 16:36