简体   繁体   中英

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.

Here's how it's laid out...

Activity -> Post

Activity has a namespace of activity and Post has a namespace of post

At the top of my Post class I have this code.

namespace Post;
use activity\activity;

That's the code that PHPStorm created when I made my class file and then extended my Activity class.

So, when I try to access my public functions inside Post, I have tried both of these methods...

\activity\post::function();

AND

$post = new \activity\post();
$post->function();

But PHPStorm tells me neither of those exist.

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.

Thanks so much for your help in understanding how this works.

Don't use \\activity , use activity .

\\activity is using the \\ (or base) namespace.

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() .

You can also define use \\activity\\activity as test and access functions like 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM