mzurhorst
mzurhorst

Reputation: 113

Bypass for cannot re-declare function

I am calling similar PHP scripts from different locations in WordPress. They all have in common that they call another php file (genlib.php) which its some kind of library with a large number of php functions.

When I ran into the "cannot re-declare function ... in genlib.php" error, I wrapped each function into an "if !function_exists" condition to avoid this.

This is ugly because I have to do it so many times.

How can I avoid this on the level where I include the genlib.php file in my scripts?

Upvotes: 0

Views: 123

Answers (2)

merlin2011
merlin2011

Reputation: 75585

I believe using include_once or require_once in all files that include the file genlib.php should solve this problem.

Update: Based on the OP's comment, it appears that this solution does not work for multiple different scripts loading libraries which include_once genlib.php. In this case, the OP may have to regress to using a guard statement wrapping the entire genlib.php.

if (!defined('GEN_LIB_PHP')) {
  define('GEN_LIB_PHP', true);
  // Rest of code for genlib.php
}

Upvotes: 4

yunzen
yunzen

Reputation: 33439

Use include_once() for the file. This prevents the system from running the code again, if the file was included before. The same applies for requrie_once().

Upvotes: 1

Related Questions