In PHP can you extend a class to the same name?
Asked Answered
G

4

14

I'm trying to find out weather I can do this or not. I have a class called Template. and I want to extend that classes functionality in another file, but I don't want to change the name.

Do I declare it as

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

   }
}

or do I just declare it like this?

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

   }
}

Gambol answered 14/7, 2009 at 23:29 Comment(0)
H
-4

You might want to check out classkit

Herzel answered 14/7, 2009 at 23:35 Comment(2)
In this case coding features change, it allow to do that, but not right & easy way. like to upgrade a software should be directly via class.Espinal
Link goes to non existent pageWaddell
C
38

Only if the parent class is in another namespace.

use SomeNamespace\Template as BaseTemplate;

class Template extends BaseTemplate 
{
     //...
}
Contract answered 21/6, 2012 at 2:50 Comment(0)
T
1

If you wanted to use a method/property of the original class you can extend it but you must use a different name, otherwise you'll get an error about redeclaring the class.

Basically:

class template {}

/* both fail with "cannot redeclare class template */

class template extends template {}

class template {}
Trimaran answered 14/7, 2009 at 23:32 Comment(0)
O
0

I am pretty sure you can not. I would suggest you do the following:

class Template extends TemplateBase
{
    // code
}

read more at: http://www.php.net/manual/en/language.oop5.php

Oman answered 14/7, 2009 at 23:32 Comment(0)
H
-4

You might want to check out classkit

Herzel answered 14/7, 2009 at 23:35 Comment(2)
In this case coding features change, it allow to do that, but not right & easy way. like to upgrade a software should be directly via class.Espinal
Link goes to non existent pageWaddell

© 2022 - 2024 — McMap. All rights reserved.