How can we redeclare a class safely in PHP?

Basically PHP doesn’t allow re-declare a class directly. If you are in need to get re-declare 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:

Page1.php

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

Page2.php

$i=10; 
for($x=0;$x<$i;$x++)
{
      require_once "Page1.php";
      $myclass = new abcd();
      $myclass->max();
}

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