[英]How do I access a subclass's functions using namespaces?
So, I have a few classes I'm trying to use PHP's namespaces with and am having some trouble accessing the functions in the lowest level class. 因此,我有一些类试图与PHP的命名空间一起使用,并且在访问最低级别的类中的函数时遇到了一些麻烦。
Here's how it's laid out... 布置方式如下...
Activity -> Post
Activity
has a namespace of activity
and Post
has a namespace of post
Activity
有一个命名空间activity
,并Post
有一个命名空间post
At the top of my Post class I have this code. 在我的Post类的顶部,我有以下代码。
namespace Post;
use activity\activity;
That's the code that PHPStorm created when I made my class file and then extended my Activity class. 那是我创建类文件然后扩展Activity类时PHPStorm创建的代码。
So, when I try to access my public functions inside Post, I have tried both of these methods... 因此,当我尝试访问Post内部的公共函数时,我已经尝试了这两种方法...
\activity\post::function();
AND 和
$post = new \activity\post();
$post->function();
But PHPStorm tells me neither of those exist. 但是PHPStorm告诉我这两个都不存在。
So, what's the actual way to access these lower level functions? 那么,访问这些较低级别功能的实际方法是什么?
I've googled quite a bit but apparently I'm not searching for the right thing because I haven't found anything about sub classes. 我已经在Google上搜索了很多,但是显然我没有在寻找正确的东西,因为我还没有找到有关子类的任何信息。
Thanks so much for your help in understanding how this works. 非常感谢您对理解其工作原理的帮助。
Don't use \\activity
, use activity
. 不要使用
\\activity
,而要使用activity
。
\\activity
is using the \\
(or base) namespace. \\activity
正在使用\\
(或基本)名称空间。
Use doesn't extend a class, it creates an alias. 使用不扩展类,它创建别名。 Since you have
Use activity\\activity
this makes it so you can access functions in the activity
class by running activity::function()
rather than using the full namespace \\activity\\activity::function()
. 由于您具有
Use activity\\activity
,因此可以通过运行activity::function()
而不是使用完整的名称空间\\activity\\activity::function()
来访问activity
类中的\\activity\\activity::function()
。
You can also define use \\activity\\activity as test
and access functions like test::function()
. 您还可以将
use \\activity\\activity as test
定义use \\activity\\activity as test
和访问函数,例如test::function()
。
I'm not sure of the point of having the namespaces the same name as the classes though but sjagr addressed that in comments. 我不确定名称空间是否与类同名,但是sjagr在注释中解决了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.