简体   繁体   中英

including the same file but from different directories

I have a problem; actually I couldn't find any solution to it.

I have a file called cron_hourly.php

This file does some tasks and is supposed to run by a cronjob; I own like 4 sites with 4 different databases that have the same schema and structure; (The 4 sites are almost the same); the 4 databases just differ in the data they hold;

I need to run this file for the 4 databases; it makes some queries to the database and then email some information. Due to some hosting restrictions; I can use only 1 cronjob; so I have 4 cron_hourly in 4 different directories that does the same task but on a different db! So they have the some functions and variables; actually they are the same except that they connect to different databases! The problem is; when I try to use the code below

     include 'path1/cron_hourly.php';
     include 'path2/cron_hourly.php';
     include 'path3/cron_hourly.php';
     include 'path4/cron_hourly.php';

I get the error 'Cannot redeclare someFunction() (previously declared in ' Actually this makes sense as the function was already declared in the same file from the other directory.

on the contrary; if I use include_once Only the first file to be included runs and the others don't! Is there any I can get around this? I want to run the four files without getting this error! By the way; if(function_exists()) won't help! because as I said before I need to re-declare the functions over and over again for each file..

What you need to do is use namespacing, see more here:

http://php.net/manual/en/language.namespaces.rationale.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM