Answer the question
In order to leave comments, you need to log in
Is it possible to do an include in a php function?
Hello, in general, the essence is this, you need to load the module in a php file, first checking the rights, etc., etc.
In the simplest version, it looks like this
index.php
<?php
$v=4;
function load_mod($name)
{
include 'PATH'.$name.'.php';
}
load_mod('mod_name');
echo $v;
?>
<?php
$v=5;
?>
Answer the question
In order to leave comments, you need to log in
Everything that is in the function disappears when it finishes working, if you do an include in the function, then at the end you need to collect all this and give it to return, then the necessary data can be used further.
Read on topics:
- use in functions (anonymous functions)
- data types in particular pointers a
working example of your code:
<?php
$v=4;
$load_mod=function ($name) use (&$v)
{
include ''.$name.'.php';
};
$load_mod('mod_name');
echo $v; // v === 5
I just checked that classes loaded inside the function can be declared outside the function, but this will not work with variables, they must be declared in global visibility.
function LLL()
{
include PRIV.DS.'classes_crm.php';
}
LLL();
$crm_cl = new ShopCRM;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question