How and where to use the "use" keyword in php

I used the "use" keyword, usually 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 realized that I need to put a line for the Specify characteristic in 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 this is because the \ Codeception \ Specify; it's a trait. But I don't understand why I couldn't reuse this trait when I set the line use \ Codeception \ Specify; before class definition?

I would be glad if someone could point me to a hint or explanation explaining to me where I should use the "use" keyword the best.

+3


source to share


3 answers


In PHP, the keyword is use

used in three cases:



+6


source


use

basically includes the class in a file to use it. There are two ways to include a class file in another file. The most common method is require

or include

. Another method is using a composer. Suppose this directory structure

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

      

There is folder A UserRegistartion.php

and you want to use the code in TestUserRegistration.php

B UserRegistration.php

It may be class

, trait

orInterface

Method 1.

In TestUserRegisteration.php

you can include

either require

file UserRegistartion.php


and use it

Method 2



Using a composer. In UserRegistration.php

you define namespace FolderA;

as the first line of code. Then write your code just like 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, where you want to include UserRegistration, you must find the relative path to the UserRegistration. So let's suppose that someday you need to change the directory structure your application will break like the relative path you provided, now it doesn't exist.

But in method 2, you always use the provided namespace \ Filename instead of where you want to use. So even you change the directory structure, you don't need to get all codes and change the path. It will work as it is. To learn more about using namespace

and composer

.

+3


source


You cannot import a class with a keyword use

. You must use the include

/ operator require

. Even if you are using any php autoloader, the autoloader will have to use either include

or require

internally.

Purpose of using the keyword:

Consider the case where you have two classes with the same name; you will find it strange, but when you work with a large MVC framework it does. Therefore, if you have two classes with the same name, put them in different namespaces. Now consider when your autoloader loads both classes (for example require

) and you are going to use the class object. In this case, the compiler will confuse which class object is loaded between the two. To help the compiler make a decision, you can use an operator use

so that it can make a decision on which to use it.

Here's the link How Keyword Usage Works

+2


source







All Articles