0

everyone

Is there a way to make this piece of code work:

<?php
    while (1) {
        include('third_party_script.php');
    }
?>

I don't know what's in third_party_script.php. It could look something like this:

<?php
    function some() { return 0;}
    some();
?>

If there is a function declaration in a third_party_script.php, I will get a "cannot redeclare" error. So I cannot use "include" o "require". Is there a solution?

Thanks in advance.

4
  • 7
    You're including a file when you don't know what it contains? That doesn't sound like a great idea. Commented Jul 8, 2011 at 10:12
  • What's your problem with this current solution? Commented Jul 8, 2011 at 10:12
  • What exactly do you want to do, And I dont like the idea of infinitely including a file. Commented Jul 8, 2011 at 10:14
  • use cURL to access that that script over and over? Commented Jul 8, 2011 at 10:16

3 Answers 3

1

As others have said, you probably don't want to be doing this.

A possible solution, however is something like this:

<?php
    while (1) {
        passthru('php third_party_script.php');
    }
 ?>

but it depends on what the third party script is doing, something like this will work:

<?php

function print_something()
{
        print "hey";
}

print_something();

?>

(although it'll lock things up if you run something like inside while(1))

but, something which needs to get at _GET variables and such-like won't.

There are loads of concerns I'd have about doing the above but it is one possible solution to the specific question you had (rather than the more general problem you're trying to solve).

Another place to look to achieve esoteric things like this is runkit:

http://www.php.net/manual/en/intro.runkit.php

but I don't have enough experience with it to "recommend" it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your reply. This is almost what I wanted. Almost, because as far as I get it, passthru will fork another process and execute the "third_party_script.php" with another php interpreter.
Yes, it will which is the reason it's a bit limited as a solution for passing data around although you could use an intermediate wrapper if all you need defined are some constants or functions available to the third party script. You pretty much can't redeclare a function or class in PHP. You could try abusing namespaces or eval to achieve what you want but I expect that'll get you in a whole heap of trouble.
0

If there is a class or a function inside you should simply call the function or instantiate the class. I wonder however why you want to run something from a script which contents you are unfamiliar with.

If you would know however, it would work like below:

<?php
    while (1) {
        include('third_party_script.php');
        //call some_function which is defined in file above
        some_function();
        //instantiate object and call some_function
        $Object = new ThirdPartyClass();
        $Object->some_function();
    }
?>

Looping code over and over without an escape will kill you're processes eventually. Eventually might not be that long in 'time' too. All in all, this sounds like a direction you do not want to pursue...

1 Comment

I'm not going to run it. It will be run by the author of the "third_party_script.php". Imagine I'm writing a library that allows executing a "third_party_script.php" over and over.
0

I'm not sire what you would be trying to do, however I'm assuming that you are trying to write a script that can run a external code segment such as a plugin, for some kind of framework.

really what you should do in such a case is inforce some structure on the plugins i.e.

the plugin code will look like

<?php
   pluginname_exec($args) {
      ...
      Plugin Code gose here
      ...
   }
?>

your script should then import the script when loading

then when ever you want to run the plugin you would call

call_user_func("{$thisplugin}_exec",$args)

where $thisplugin is a variable containing you plugin name, most likely the file name, with out the .php

this way you only include the file once, but can call the code as many times as you need.

If your trying to execute some unknown code for reasons other the a plugin system, it's most likely a bad idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.