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

时间:2013-06-28 18:49:54

标签: php class namespaces extends

假设我有一个班级:

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

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

namespace Custom;

class Person extends \Person
{
  public function doSomethingElse()
  {
    // ...
  }
}

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

1 个答案:

答案 0 :(得分:2)

使用相同的类名或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.