1

The requirement is to run multiple PHP files from one single PHP File without any timeouts, having searched stack overflow the only solution seems to be using EXEC however this is not allowed on my host.

If I run all.php from CLI then it should run 1.php , 2.php and 3.php.

Now normally this would be straight forward and I would just require or include them into all.php however the function names in 1.php , 2.php and 3.php match and I am looking for a solution which runs the file and then forgets about functions, variables or anything else within the file.

Essentially I want 1.php , 2.php , 3.php to run separately and All.php is only needs to trigger these files.

One possible solution in my mind was using curl in all.php to trigger them files, however this would introduce webserver limits and timeouts which I am not looking to deal with.

Thanks for the help guys. Apologies if the question is not very clear.

12
  • Can you share us the usecase? Why you need to run multiple PHP files? Commented May 29, 2020 at 17:03
  • Well , I can envisage using this solution in multiple situations but one could be that if I wish to run multiple files on the CLI quickly then I can just run all.php and the rest will run automatically. Commented May 29, 2020 at 17:06
  • Why you are trying to achieve this since PHP is blocking Scripting Language? I think the real approach for this is threading model. But I am not still convinced why you should run in parallel. You haven't given any reason you just mentioned you should run PHP scripts in parallel. Commented May 29, 2020 at 17:11
  • As I said earlier, its for ease of use. If I want to trigger the same 10 files from the CLI instead of having to manually trigger each file this solution lets me trigger one file which then triggers the required 10 files. I dont need them to run at the same time , the files can run one by one . Commented May 29, 2020 at 17:14
  • Why not you can't use class and its methods to trigger serially? Commented May 29, 2020 at 17:16

2 Answers 2

1

You can use CURL to run them sequentially or CURL_MULTI if you want them to run in parallel. PHP CURL documentation

A very simplified example:

<?php

$ch = curl_init("http://localhost/1.php");
curl_exec($ch);
curl_close($ch);

$ch = curl_init("http://localhost/2.php");
curl_exec($ch);
curl_close($ch);

$ch = curl_init("http://localhost/3.php");
curl_exec($ch);
curl_close($ch);
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Sam, I was also looking at using CURL. If CURL is used in this way and the initial file is triggered over CLI would I encounter timeout issues if one of the files takes too long? - Would apache cause me any issues?
Iff you're worried abnout timeouts, but don't care if any one file fails, you can use curl_multi. That would kick them all off at the same time. Take a look here: php.net/manual/en/function.curl-multi-init.php You can also increase timeout settings for each script - php.net/manual/en/function.set-time-limit.php
In my use case I cant have one file failing. Even if curl is run over CLI and it is accessing a local file does this still introduce APACHE?
Yes, you are making an HTTP call, so your web server will be used.
Appreciate the answer Sam, however I am not looking to introduce the web server into the situation. Using just CLI ensures that I don't have to deal with web server timeouts. I was hoping for an answer without having to deal with the web server timeouts.
|
0

From a comment on the question: Not looking for them to run at the same time. All the files can run one after another. I just need to avoid a fatal error due to the same function names withing the multiple files.

THREEE OPTIONS

Option 1, put the shared functions into a separate file:

<?php

require shared_functions.php

require 1.php
require 2.php
require 3.php

?>

Option 2, wrap function declarations inside a check to see whether the function has been declared already. Requires placing wrapped function declarations before code that calls them:

1.php

<?php

if (!function_exists('shared_function1')):
  function 1(){
      // yadda yadda
  }
endif;

// rest of 1.php
?>

2.php

<?php

if (!function_exists('shared_function1')):
  function 1(){
      // yadda yadda
}
endif;

// rest of 2.php
?>

If the only problem you are encountering is bombs on function delcarations for functions that already have been declared by another script, make the function names unique (explained at end of this option), then run the scripts with this:

Option 3 - all.php

<?php

$scripts_to_run=array();
$scripts_to_run[]=1.php;
$scripts_to_run[]=2.php;
$scripts_to_run[]=3.php;
.
.
.
$scripts_to_run[]=10.php;

foreach ($scripts_to_run as $script):
  require $script;
endforeach;

*TO MAKE THE FUNCTION NAMES UNIQUE, in 1.php, for each sharedfunctionname, search/replace sharedfunctionname with functionname_1 In 2.php, for each sharedfunctionname, search/replace sharedfunctionname with functionname_2 ... etc.

examples: CURRENT SCRIPTS

1.php
<?php

$var_dry   = 'dry';
$var_wet   = 'wet';
$result=sharedfunctionname($var_dry, $var_wet);

function sharedfunctionname($var_1, $var_2){
   // mix two strings to make one with the letters from both intermingled
}

2.php
<?php

$var_dog   = 'dog';
$var_cat   = 'cat';
$result=sharedfunctionname($var_dog, $var_cat);

function sharedfunctionname($var_1, $var_2){
   // something completely different from mixing two strings....
}

AFTER MAKING FUNCTION NAMES UNIQUE

1.php

<?php

$var_dry   = 'dry';
$var_wet   = 'wet';
$result=functionname_1($var_dry, $var_wet);

function functionname_1($var_1, $var_2){
   // mix two strings to make one with the letters from both intermingled
}

2.php
<?php

$var_dog   = 'dog';
$var_cat   = 'cat';
$result=functionname_2($var_dog, $var_cat);

function functionname_2($var_1, $var_2){
   // something completely different from mixing two strings....

}

2 Comments

Hi , Unfortunately I cannot extract the functions from the files and as for option 2 the functions causing this error are only named similarly the actual function may be completely different. Now one may say just rename the function but imagine there are 10 files with various functions this would take some time to sort out.
Ah, so you have shared function names assigned to functions doing different things. Can you make the function names unique? This is what I mean: in 1.php, for each sharedfunctionname, search/replace sharedfunctionname with functionname_1 In 2.php, for each sharedfunctionname, search/replace sharedfunctionname with functionname_2 ... etc. Then, use option 3 in this answer, which I am adding as soon as I post this comment. Hope it helps. You seem to know what you are doing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.