简体   繁体   English

PHP使用名称空间扩展

[英]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: Test.php的类过去看起来像这样:

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: 也就是说,我也在IService中尝试过执行以下操作:

 namespace library\App\Service

 class IService

and change Test.php to 并将Test.php更改为

 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. 因此,您需要实现一个自动加载器,该自动加载器将名称空间映射到文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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