How to use include within a function?
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.
PHP Use Include Inside Function
That use case is explicitly documented:
If the include occurs inside a function within the calling file, then
all of the code contained in the called file will behave as though it
had been defined inside that function. So, it will follow the variable
scope of that function. An exception to this rule are magic constants
which are evaluated by the parser before the include occurs.
IMHO, it's way simpler to keep base paths in constants (you already seem to be doing it to some extent) or even make a full-site search and replace (which is a 30 second task in any decent editor) than rewriting all your included files to use global variables.
PHP include/require inside functions
While @Luceos answer is technically correct (the best kind of correct), it does not answer the question you asked, namely is doing this better, or do includes happen regardless of function calls?
I tested this in the most basic way (OP, why didn't you?):
<?php
echo "Testing...";
function doThing() {
include nonExistantFile.php;
}
//doThing();
echo "Done testing.";
Results:
if I call doThing();
I get a file not found warning.
If I comment out doThing();
... there is no error! So you can save file load time by doing this.
c++: Is it possible to #include inside a function body?
No.
You've got it a bit wrong; #include
is not processed at run-time, at all. It's not possible to #include
a file based on a program's execution characteristics; once the program executes its source is fixed (since it's already compiled).
PHP include/require within a function
You can return data from included file into calling file via return
statement.
include.php
return array("code" => "007", "name => "James Bond");
file.php
$result = include_once "include.php";
var_dump("result);
But you cannot call return $something;
and have it as return statement within calling script. return
works only within current scope.
EDIT:
I am looking to do this as I have lots
of functions in separate files and
they all have a large chunk of shared
code at the top.
In this case why don't you put this "shared code" into separate functions instead -- that will do the job nicely as one of the purposes of having functions is to reuse your code in different places without writing it again.
Related Topics
Like Query Using Multiple Keywords from Search Field Using Pdo Prepared Statement
Mysqli_Query() Expects at Least 2 Parameters, 1 Given In
How to Define an Empty Object in PHP
PHP Prepend Leading Zero Before Single Digit Number, On-The-Fly
Prevent Nginx 504 Gateway Timeout Using PHP Set_Time_Limit()
How to Explode and Trim Whitespace
Get Woocommerce Product Categories from Wordpress
How to Determine the Memory Footprint (Size) of a Variable
Laravel 4: How to "Order By" Using Eloquent Orm
How to Remove Part of a String in PHP
MySQL Performance - "In" Clause VS. Equals (=) for a Single Value
Highlight the Word in the String, If It Contains the Keyword
Setting Up Ssl on a Local Xampp/Apache Server
Passing Multiple PHP Variables to Shell_Exec()