开发者

php: how to execute script from other script over and over

everyone

Is there a way to make this piece o开发者_JAVA百科f 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.


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.


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜