php PHP如何调用包含的文件函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3508517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP how to call included file functions
提问by Moon
lets say i make a file Services.php
假设我制作了一个文件Services.php
<?php
$myGlobalVar='ranSTR';
function serv1($num)
{
//some processing...
return $num;
}
function serv2($num)
{
//some processing...
return $num;
}
?>
and i include it in some other file lets say myFile.phpby
我包括在其他一些文件可以说myFile.php通过
include "Services.php";
OR
或者
require "Services.php";
- How can i call its functions in myFile.php
- 我如何在myFile.php 中调用它的函数
回答by jordanstephens
You can just call any of these functions. After the file is included, the functions will be placed in the global scope.
您可以只调用这些函数中的任何一个。包含文件后,函数将放置在全局范围内。
have you not tried:
你有没有试过:
include "Services.php";
$num = 123;
serv1($num);
From the doc:
从文档:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
当一个文件被包含时,它包含的代码继承了包含发生所在行的变量范围。从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用。但是,包含文件中定义的所有函数和类都具有全局作用域。
Check out these two doc resources for more information on what you're having trouble understanding:
查看这两个文档资源,以获取有关您理解困难的更多信息:
- include statement: http://php.net/manual/en/function.include.php
- variable scope: http://php.net/manual/en/language.variables.scope.php
回答by Thanatos
Just call them the same as you would any other function:
只需像调用任何其他函数一样调用它们即可:
serv1(42);
回答by Chiggins
You should just be able to call serv2(3)
or whatever need be.
你应该能够打电话serv2(3)
或任何需要。