Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 733555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:16:15+00:00 2026-05-14T07:16:15+00:00

I have a large function that I wish to load only when it is

  • 0

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';
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:16:15+00:00Added an answer on May 14, 2026 at 7:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty large object that I need to pass to a function
I have a large non static JAVA function, which uses this reference at several
I have a several functions which are quite large, and are only used in
I have a large script that is very synchronous. 1) it runs a function
I have a function that processes a large image. By the specification, the largest
In Cython, say I have a C function that returns a large buffer allocated
I have a function that strips out lines from files. I'm handling with large
Quick question: If I have a very large function/sub in a class that is
I have a large PHP class that has a large variable/function declaration and I
I have a large function (I know, see below) that breaks proguard during its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.