如何在函数中使用include? [英] How to use include within a function?

查看:132
本文介绍了如何在函数中使用include?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的功能,我希望只在需要时才加载。所以我认为使用include是最好的选择。但是我需要几个支持函数,以及在go_do_it()中使用的。



如果它们在包含文件中,我会得到一个redeclare错误。请参阅示例A



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

如果我为func_1代码使用include_once,那么第二次调用将失败。



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



示例A:

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

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

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

例子B:

 <?php 
/ * main.php * /
go_do_it();
go_do_it();
函数go_do_it(){
include_once'func_2.php';
包含'func_1.php';
}
?>

<?php
/ * func_1.php * /
echo'< br> - 正在做';
nested_func();
?>

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


解决方案

使用的问题包括()是一个函数:

  include'file1.php'; 

函数include2(){
include'file2.php';

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



现在所有函数都是全局函数,但变量不是。我并不惊讶这与 include_once 有关。如果你真的想这样做 - 老实说,我不会 - 你可能需要借用一个旧的C / C ++预处理器技巧:

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

// code here
}

如果您想要去延迟加载的方式(这可能有操作码缓存问题)使用自动加载。


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

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

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

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

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.

Example 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:

<?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';
}
?>

解决方案

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

include 'file1.php';

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

file1.php will have global scope. file2.php's scope is local to the function 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:

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.

这篇关于如何在函数中使用include?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆