php - 在 PHP 中使用命名空间扩展类时,我应该为类使用相同的名称吗?

标签 php class namespaces extends

假设我有一个类:

class Person
{
  public function doSeomthing()
  {
    // ...
  }
}

我想使用我自己的命名空间扩展这个类以添加额外的功能。使用相同的类名是好还是坏?例如:

命名空间自定义;
class Person extends \Person
{
  public function doSomethingElse()
  {
    // ...
  }
}

我对 PSR 标准对此有何看法特别感兴趣。

最佳答案

使用相同的类名,或 Name collisions是命名空间专门用来解决的问题之一。http://www.php.net/manual/en/language.namespaces.rationale.php

In the PHP world, namespaces are designed to solve two problems that authors 
of libraries and applications encounter when creating re-usable code elements 
such as classes or functions:

1.Name collisions between code you create, and internal PHP 
classes/functions/constants or third-party classes/functions/constants.

2. Ability to alias (or shorten) Extra_Long_Names designed to alleviate 
the first problem, improving readability of source code.

关于php - 在 PHP 中使用命名空间扩展类时,我应该为类使用相同的名称吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371638/

相关文章:

css - HTML5 和 Bootstrap 类 ="container",它可以应用于 body 还是只应用于 div?

wpf - UserControl 中过度指定的命名空间失败 WPF 构建

php - 将 PHP 编译成独立的可执行文件(更快?)

php - 使 fetch_assoc 完全匹配 fetch_all

C++ typedef 结构与类

C++ 从另一个类调用一个函数

xml - 使用 XSL 向 XML 添加命名空间 + 前缀

visual-studio-2012 - 尝试在 Visual Studio C# 中使用 Microsoft.Kinect 命名空间

php - Laravel - 插入由缺少列的 create() 方法生成的查询

php - mysql_query() 期望参数 2 是资源, bool 值给出