简体   繁体   中英

PHP Extends using namespaces

I'm new to using namespaces and am trying to re-write some old code to use namespaces.

My current folder structure looks like this:

library
| - App
| - - Service
| - - - IService.php
| - MyApp
| - - Service
| - - - Test.php

The class for Test.php used to look like this:

class MyApp_Service_Test extends App_Service_IService

but I'm trying to implement namespaces and have adjusted it to:

namespace library\MyApp\Service

class Test extends library\App\Service\IService

However doing that my error logs are showing the following:

Fatal: Class 'library\MyApp\Service\library\Api\Service\IService' not found

That said I also have tried in IService to do:

 namespace library\App\Service

 class IService

and change Test.php to

 class Test extends IService

I know I'm doing it completely wrong but some direction would be greatly appreciated!

You are making the assumption that there is a relation between the folder structure and namespaces. That is not true. Namespaces are a way to organize classes, and to make it possible to define the same class name in different namespaces.

That said, your can establish a relation between your classes and your namespaces. Therefore you need to implement a autoloader, that maps namespaces into folders.

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