PHP : Does extending class need another 'use' to call namespace?

早过忘川 提交于 2019-12-17 19:57:39

问题


I'm wondering whether in the situation where I'm extending a class that has already 'use' keyword above it to use specific namespace - do I need to add another 'use' above the inheriting class to use the same namespace? Situation like this:

namespace Core;

use System\Plugin;

class Front extends Application { }

and now in the Controller, which is called directly without the namespace (using full path):

use System\Plugin;

class PageController extends Front { }

or would it work without 'use' as well and allow me to use the System\Plugin namespace:

class PageController extends Front { }

?


回答1:


No, you need the "use" statement in both files. Use is a file-level keyword and isn't affected by inheritance.

See the scoping rules for importing and the little box describing what I said at the bottom of the manual page.



来源:https://stackoverflow.com/questions/11794901/php-does-extending-class-need-another-use-to-call-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!