How and where should I use the keyword “use” in php

后端 未结 3 1626
情话喂你
情话喂你 2021-01-14 06:41

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



        
3条回答
  •  [愿得一人]
    2021-01-14 07:08

    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

提交回复
热议问题