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?

0

1 Answer 1

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'; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks , but this this just runs the example.php once. I need to run multiple instances of example.php in the same code.
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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.