Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use include within a function?

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';
}
?>
like image 200
Mahks Avatar asked Apr 12 '10 02:04

Mahks


People also ask

Can I #include inside function?

While you can put a #include inside a function body, it is not what you want to do. There is no way to include a header file only if a certain function is called because the include happens before compile time, but the function call happens at runtime.

How do you use include function?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is the difference between the include () and require () functions?

include() Vs require() The only difference is that the include() statement generates a PHP alert but allows script execution to proceed if the file to be included cannot be found. At the same time, the require() statement generates a fatal error and terminates the script.

How can we include a file inside another file in PHP?

The Include() function is used to put data of one PHP file into another PHP file. If errors occur then the include() function produces a warning but does not stop the execution of the script i.e. the script will continue to execute.


1 Answers

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.

like image 82
cletus Avatar answered Oct 14 '22 09:10

cletus