简体   繁体   中英

Should I use the same name for a class when extending it with a namespace in PHP?

Let's say I have a class:

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

I want to extend this class to add extra functionality, using my own namespace. Is it a good or bad idea to use the same class name? For example:

namespace Custom;

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

I'm particularly interested as to what PSR standards might feel about this.

Using the same class names, or Name collisions is one of the problems that namespaces are specifically designed to solve.. 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.

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