1

I need to include a reference to another php file example.php several times throughout the code, but i get "PHP Fatal error: Cannot redeclare class error."

<?php include 'example.php'; ?>

How to accomplish this?

Henry The Least
  • 619
  • 3
  • 11
  • 20

1 Answers1

5

Change it to:

<?php include_once 'example.php'; ?>

If your example.php includes a class and other code, separate it into one file for the class, and the other file for the code you want to execute multiple times. Then in the page where you call the above code, you could write:

<?php include_once 'exampleClass.php'; ?>
<?php include 'exampleCode.php'; ?>
<?php include 'exampleCode.php'; ?>
Jodes
  • 14,118
  • 26
  • 97
  • 156
  • Thanks , but this this just runs the example.php once. I need to run multiple instances of example.php in the same code. – Henry The Least Mar 02 '13 at 06:46
  • 2
    You can only include a class once. You need to separate example.php into two files. One with the class which you include and run once, and another file with the code which uses the class (which you then include multiple times) – Jodes Mar 02 '13 at 06:47