How to use include within a function?
- 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().
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.
-func_1 needs include
-func_2 needs include_once
Why does does include_once fail for func_1, does it get reloaded each time the function is called?
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';
}
?>