简体   繁体   English

如何使用名称空间访问子类的函数?

[英]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.

相关问题 如何从子类的实例调用父类的函数? - How do I call the functions of the parent class from an instance of a subclass? 如何从子类访问父属性? PHP - How do I access parent property from subclass? PHP 如何在视图中使用键值访问数组值 - How do I access an Array Value using it's Key in a view 如何用SimpleXML替换命名空间? - How Do I Replace Namespaces with SimpleXML? 如何在自动加载中使用 PHP 命名空间? - How do I use PHP namespaces with autoload? 如何在 phalcon 框架中使用命名空间? - How do i use namespaces in phalcon framework? 如何使用Ajax调用使用PHP的“ include”包含的javascript函数? - How do I call javascript functions included using PHP's 'include' using ajax? 如何使用 HTML 帮助程序的函数在 codeigniter 中编写以下 html 代码? - How do I write the following html code in codeigniter using HTML helper's functions? 如何使用名称空间(在参数和方法中)和嵌套结构(在参数中)在PHP中调用SOAP方法? - How do I call SOAP methods in PHP using namespaces (in params and methods) and nested structures (in params)? PHP命名空间:如果我不需要重新声明已经使用的函数,是否需要它们? - PHP Namespaces: If I'm not going to need to redeclare already taken functions do I need them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM