2013-06-28 46 views
1

比方說,我有一個類:在PHP中使用名稱空間擴展類時,我應該使用相同的名稱嗎?

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

我要擴展這個類來添加額外的功能,用我自己的命名空間。使用相同的類名稱是好的還是壞主意?例如:

命名空間自定義;

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

我對PSR標準可能對此感覺特別感興趣。

+0

只要是你的命名空間,我認爲,因爲它讓你越應該命名感覺到你。 – Twisted1919

回答

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. 
相關問題