【问题标题】:Should I use the same name for a class when extending it with a namespace in PHP?在 PHP 中使用命名空间扩展类时,我应该为类使用相同的名称吗?
【发布时间】:2013-06-28 18:49:53
【问题描述】:

假设我有一堂课:

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

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

命名空间自定义;

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

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

【问题讨论】:

  • 只要它是您的命名空间,我认为您应该命名它,因为它对您更有意义。

标签: php class namespaces extends


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-01-08
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 2013-10-18
    • 2010-12-20
    • 2010-12-05
    相关资源
    最近更新 更多