通过HTTP从另一个PHP脚本调用函数:它是更慢还是别的什么? [英] Calling a function from another PHP script via HTTP: is it slower or anything else?
问题描述
假设我有一个PHP文件 some_function.php
它可以运行 file_get_contents('some_function.php?'。$ parameters_string)
(或任何类似的函数)。此函数的参数可以通过GET或POST HTTP方法给出。
而是可以 include
并在一个脚本中使用此函数。
我可以想出,如果我需要运行一个单独的进程或者我需要这个函数在一个单独的服务器。但如果不是,有什么理由不这样做吗?可能这个调用会慢很多吗?还有我应该考虑的吗?
我知道我不能使用全局变量(我认为是一个坏的编码风格)。
通过使用 file_get_contents()
,你不会实际调用相关函数但会使 HTTP
请求传递一些预定义的参数,然后在您的代码中传递该函数。
使用 include()
可以有一个类库
或函数
,并根据需要直接调用它们。
strong>示例:
> function my_function_1(){}
function my_function_2(){}
index.php
include('library.php');
my_function_1(); //调用第一个函数
my_function_2(); //调用第二个函数
my_function_1(); //再次调用第一个函数,只是因为我们可以
你不能这样做通过 HTTP
请求,即使您硬编码
您的 some_function.php
文件做一些功能,如上所述,你会得到真正坏的代码,将很难定制您的需求,几乎不可能维持一旦它变得更大。
Let's say I have a PHP file some_function.php
which I can run with file_get_contents('some_function.php?' . $parameters_string)
(or any similar function). The parameters to this function can be given via either GET or POST HTTP method.
Instead I could include
needed file and use this function within one script.
I could figure out that it could be reasonable if I need to run a separate process or I need this function to be on a separate server. But if not, is there any reasons not to do it? May be this call will be much slower? Anything else I should take into account?
I know that I will not be able to use global variables (which I assume as a bad coding style anyway).
By using file_get_contents()
you will not be actually calling the function in question but will make an HTTP
request passing some predefined parameters which will then be passed on the function in your code.
Using include()
you could have a library of classes
or functions
inside that file, and call them directly as needed and as many times as needed.
EXAMPLE:
library.php
function my_function_1() { }
function my_function_2() { }
index.php
include('library.php');
my_function_1(); // call the first function
my_function_2(); // call the second function
my_function_1(); // call the first function again, just because we can
You wouldn't be able to do that through the HTTP
request and even if you did hardcode
your some_function.php
file to do some functionality like above, you would end up with really bad code that would be hard to customize to your needs and near impossible to maintain once it gets bigger.
这篇关于通过HTTP从另一个PHP脚本调用函数:它是更慢还是别的什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!