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.
In PHP, the keyword
use
is used in 3 cases:You can not import class with
use
keyword. You have to useinclude
/require
statement. Even if you use some php auto loader, still autoloader will have to use eitherinclude
orrequire
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 theuse
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
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 isrequire
orinclude
method. Another method is using composer. Assume this Directory StructureIn Folder A there is
UserRegistartion.php
and you want to use the code inTestUserRegistration.php
InUserRegistration.php
It can beclass
,trait
orInterface
Method 1.
In
TestUserRegisteration.php
you caninclude
orrequire
fileUserRegistartion.php
and use it
Method 2
Using Composer. In
UserRegistration.php
you definenamespace FolderA;
as the first line of code. Then write your code as you do. So Now you want to use this file inTestUserRegistration.php
you doWhich 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
andcomposer
.