代码之家  ›  专栏  ›  技术社区  ›  Matt Lima

在PHP中,你可以将类扩展为相同的名称吗?

  •  14
  • Matt Lima  · 技术社区  · 17 年前

    class template extends template
    {
       // code here
       function foo()
       {
    
       }
    }
    

    class template
    {
       // write function
       function foo()
       {
    
       }
    }
    

    4 回复  |  直到 14 年前
        1
  •  37
  •   Samuel Katz Jon Skeet    14 年前

    仅当父类位于另一个命名空间中时。

    use SomeNamespace\Template as BaseTemplate;
    
    class Template extends BaseTemplate 
    {
         //...
    }
    
        2
  •  1
  •   Ross    17 年前

    如果你想使用原始类的方法/属性,你可以扩展它,但必须使用不同的名称,否则你会得到一个关于重新声明类的错误。

    基本上:

    class template {}
    
    /* both fail with "cannot redeclare class template */
    
    class template extends template {}
    
    class template {}
    
        3
  •  0
  •   farinspace MikeAr    17 年前

    我敢肯定你不能。我建议你做以下事情:

    class Template extends TemplateBase
    {
        // code
    }
    

    http://www.php.net/manual/en/language.oop5.php

        4
  •  -4
  •   joebert    17 年前

    你可能想看看 classkit

    推荐文章