php 如何在函数中使用include?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2619573/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:08:22  来源:igfitidea点击:

How to use include within a function?

phpfunctioninclude

提问by Mahks

I have a large function that I wish to load only when it is needed. So I assume using include is the way to go. But I need several support functions as well -only used in go_do_it().

我有一个大函数,我希望仅在需要时加载。所以我认为使用 include 是可行的方法。但我也需要几个支持函数——只在 go_do_it() 中使用。

If they are in the included file I get a redeclare error. See example A

如果它们在包含的文件中,我会收到重新声明错误。参见示例 A

If I place the support functions in an include_once it works fine, see Example B.

如果我将支持函数放在 include_once 中,它可以正常工作,请参见示例 B。

If I use include_once for the func_1 code, the second call fails.

如果我将 include_once 用于 func_1 代码,则第二次调用将失败。

I am confused as to why include_once causes the function to fail on the second call, it seems to not 'see' the code the second time but if nested functions are present, it does 'see' them.

我很困惑为什么 include_once 导致函数在第二次调用时失败,它似乎第二次没有“看到”代码,但如果存在嵌套函数,它会“看到”它们。

Example A:

示例 A:

<?php
/*  main.php    */
go_do_it();
go_do_it();
function go_do_it(){
    include 'func_1.php';
}
?>

<?php
/*  func_1.php  */
echo '<br>Doing it';
nested_func()

function nested_func(){
    echo ' in nest';
}
?>

Example B:

示例 B:

<?php
/*  main.php    */
go_do_it();
go_do_it();
function go_do_it(){
    include_once 'func_2.php';
    include 'func_1.php';
}
?>

<?php
/*  func_1.php  */
echo '<br> - doing it';
nested_func();
?>

<?php
/*  func_2.php  */
function nested_func(){
    echo ' in nest';
}
?>

回答by cletus

The problem with using include()within a function is that:

include()在函数内使用的问题是:

include 'file1.php';

function include2() {
  include 'file2.php';
}

file1.phpwill have global scope. file2.php's scope is local to the function include2.

file1.php将具有全球范围。file2.php的作用域是函数的本地范围include2

Now all functions are global in scope but variables are not. I'm not surprised this messes with include_once. If you really want to go this way—and honestly I wouldn't—you may need to borrow an old C/C++ preprocessor trick:

现在所有函数在范围内都是全局的,但变量不是。我并不感到惊讶这弄乱了include_once. 如果你真的想走这条路——老实说我不会——你可能需要借用一个旧的 C/C++ 预处理器技巧:

if (!defined(FILE1_PHP)) {
  define(FILE1_PHP, true);

  // code here
}

If you want to go the way of lazy loading (which by the way can have opcode cache issues) use autoloading instead.

如果您想采用延迟加载的方式(顺便说一下,可能存在操作码缓存问题),请改用自动加载。

回答by meagar

I have a large function that I wish to load only when it is needed. So I assume using include is the way to go.

我有一个大函数,我希望仅在需要时加载。所以我认为使用 include 是可行的方法。

Your base assumption is wrong. This kind of optimization is counter-productive; even if your function is hundreds of lines long there will be no noticeable benefit to hiding it from PHP's parser. The cost for PHP to parse a file is negligible; real noticeablespeed gains come from finding better algorithms or from better ways to talk to your database.

你的基本假设是错误的。这种优化适得其反;即使您的函数长达数百行,将它隐藏在 PHP 的解析器中也没有明显的好处。PHP 解析文件的成本可以忽略不计;真正显着的速度提升来自于找到更好的算法或更好的与数据库对话的方式。

That said, you should be including the function definition in the included file. Rather than moving the body of the function into func_1.php, move the entirefunction into the file. You can then require_oncethe file containing the function inside each file where you need it, and be sure that it is included exactly once regardless of how many times you attempt to include it.

也就是说,您应该在包含的文件中包含函数定义。与其将函数体移动到 中func_1.php,不如将整个函数移动到文件中。然后require_once,您可以将包含该函数的文件放在您需要的每个文件中,并确保无论您尝试包含多少次,它都只包含一次。

An example:

一个例子:

file1.php

文件1.php

function test() {

}

index.php

索引.php

require_once('file1.php');

include('table_of_contents.php');
test();

回答by amir hossieni

Hi I solved the problem

嗨,我解决了问题

//connect.php


$connect ="OK";

and

include "connect.php";

show($connect);

function show($connect){


echo $connect;


}