Take the 2-minute tour ×

We all know the infamous "cannot redeclare class" error. Is there any method to overcome this and actually declare a new class with the same name, or is this impossible in PHP 5?

share|improve this question
What are you trying to accomplish, might make the answers more useful. Otherwise check out Kohana's class system. –  David Apr 17 '10 at 15:33
Why on earth would you want to do that? –  JSBձոգչ Apr 17 '10 at 15:40
Sounds like an XY problem. –  rjh Apr 17 '10 at 15:51
add comment

6 Answers

There may be a way using some obscure extension, but in basic standard PHP, as far as I know, no.

You can, however, always extend an existing class and - maybe, depending on your scenario - "smuggle" an instance of that extended class into the application you're working on.

share|improve this answer
add comment

As Pekka and Techpriester both pointed out: no, you cannot. However, if you're using PHP >= 5.3, then you can use namespaces and the "use" construct to effectively "redeclare" the class. Here's an example:

// MyClass.php
class MyClass {
  const MY_CONST = 1;
// MyNamespaceMyClass.php namespace Mynamespace; class MyClass { const MY_CONST = 2; }
// example.php require_once 'MyClass.php'; require_once 'MyNamespaceMyClass.php';
use Mynamespace\MyClass as MyClass;
echo MyClass::MY_CONST; // outputs 2

Thus, you've got your desired result, as MyClass now refers to your namespaced class.

share|improve this answer
+1 nice workaround for those situations where there is no other way (3rd party software, etc.) –  Pekka 웃 Apr 17 '10 at 17:06
add comment

It is impossible. Depending on the use case, namespaces, like jpfuentes2 mentioned, might work for you.

One hack is to implement a custom new "operator".


$GLOBALS['class_map'] = array('A' => 'A');
function _new($class){
  $realClass = $GLOBALS['class_map'][$class];
  return new $realClass;

class A {} $a = _new('A');

// change the implementation $GLOBALS['class_map']['A'] = 'B'; $a2 = _new('A');

Another hack is to use runkit to re-implement a class.

share|improve this answer
add comment

AFAIK, redeclaring exiting functions or classes is not possible in PHP.

If you could tell, what you are trying to do, maybe there's another solution ...

share|improve this answer
add comment

Basically you cannot redeclare a class. But if you really want to, you can. :) Everything is possible. Need a class, that changes it's structure dynamically? You can use magic method __call and pattern State.

class Example
  var $state;

  public function setImplementation($state)
    $this->state = $state;

  public function __call($method, $args)
    if (method_exists($this->state, $method))
       return $this->state->$method($args);
      // error


There is also a PHP toolkit to play with classes dynamically: http://php.net/manual/en/book.runkit.php

I know that redeclaring class and its methods is possible in Ruby (and I would consider it as a mistake in a language design).

share|improve this answer
add comment

Basically we can't redeclare a class in PHP directly. If you are in need to get redeclare a class in php, then I suggest you to write that class in a separate file and use require_one to call that file to the desired page. It's as follows:


class abcd
    function max()
        echo "Hello World!!! count:-".$GLOBALS['x'];


      require_once "Page1.php";
      $myclass = new abcd();

Now it will work as you desired. It worked for me.

The output will be as follows :

 Hello World!!! count:- 0

 Hello World!!! count:- 1

 Hello World!!! count:- 2

 Hello World!!! count:- 3

 Hello World!!! count:- 4

 Hello World!!! count:- 5

 Hello World!!! count:- 6

 Hello World!!! count:- 7

 Hello World!!! count:- 8

 Hello World!!! count:- 9
share|improve this answer
add comment

Your Answer


