PHP:扩展类是否需要另一个“use”来调用命名空间? [英] PHP : Does extending class need another 'use' to call namespace?

查看:431
本文介绍了PHP:扩展类是否需要另一个“use”来调用命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在这种情况下,我扩展一个类已经使用关键字上面它使用特定的命名空间 - 我需要添加另一个'use'上面的继承类使用相同的命名空间?这样的情况:

 命名空间核心; 

使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆