PHP:扩展类是否需要另一个“use”来调用命名空间? [英] PHP : Does extending class need another 'use' to call namespace?
问题描述
命名空间核心;
使用System \Plugin;
class Front extends Application {}
直接调用没有命名空间(使用完整路径):
使用System\Plugin;
class PageController extends Front {}
'允许我使用System \Plugin命名空间:
class PageController extends Front {}
?
解决方案不,你需要在两个文件中的使用语句。 use是一个文件级关键字,不受继承影响。
请参阅导入范围规则>和小盒子描述我在手册页底部说的内容。
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 { }
?
解决方案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.
这篇关于PHP:扩展类是否需要另一个“use”来调用命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!