include_once does not have access to properties, variables or functions of the included file

-1

I am creating a generic file where multiple systems can use the same libraries, thus generating a much smaller package. In this generic file there is a function where I include the necessary files. The problem that is occuring is that when I do the system includes in this function, I do not have access to the properties, variables and functions of the included file. I'll try to exemplify the structure of the system.

The structure of the system is:

  • wamp/www/sistem/exemploSistema/index.php

  • wamp/www/sistem/exemploSistema/funcoes.php

  • wamp/www/sistem/generics/funcaoGenerica.php

File funcoes.php :

<?
   $tt = 'teste';
   function teste(){
       global $tt;
       return $tt;
   }
?>

File funcaoGenerica.php :

<?php
    function generic($sting){
        include_once($sting);
    }
?>

File index.php :

<?
   include_once('/sistem/generics/funcaoGenerica.php');
   generic('/sistem/exemploSistema/funcoes.php');
   echo teste();
?>

In the index.php file I can not access the teste function of the funcoes.php file. Would you have a solution to this problem or some other way to it?

    
asked by anonymous 26.08.2014 / 21:18

4 answers

2

It is a scope problem. $tt has to be declared as global in function generic within funcaoGenerica.php .

function generic($sting){
    global $tt;
    include_once($sting);
}

I suspect that there is a more elegant way to do this, but I could not say now ... I found some slightly related topics, but with interesting material:

26.08.2014 / 22:31
0

In a normal function, the include or the require is variable scope ie the file is only available within the scope of the calling function.

  

When a file is included, the code it contains inherits the variable   scope of the line on which the include occurs. Any variables available   at what line in the calling file will be available within the called   file, from that point forward. However, all functions and classes   defined in the included file have the global scope.

link

For your code to work it should look like this:

File funcoes.php:

<?
   $tt = 'teste';
   function teste(){
       global $tt;
       return $tt;
   }
?>
Arquivo funcaoGenerica.php:

<?php
    function generic($sting){
        include_once($sting);

       return test();

    }
?>
Arquivo index.php:

<?
   include_once('/sistem/generics/funcaoGenerica.php');

   echo  generic('/sistem/exemploSistema/funcoes.php');
?>

Maybe instead of a function create a file with the list of all the files you need. Something like:

<?php

set_include_path(wamp/www/sistem/exemploSistema/);
set_include_path(wamp/www/sistem/generics/);

require(index.php);
require(funcoes.php);
require(funcaoGenerica.php);

?>

I have never tested this type of file, but it can be a solution.

    
27.08.2014 / 00:14
0

You need to imagine that include , does nothing more than include the contents of that file in that location, so if you are inside a function, it will be under the scope of it.

For example, if you have:

<?php
    function generic(){
        include_once('funcoes.php');
    }
?>

And the file funcoes.php :

<?
   $tt = 'teste';
   function teste(){
       global $tt;
       return $tt;
   }
?>

It is exactly the same thing as:

<?php
    function generic(){
       $tt = 'teste';
       function teste(){
           global $tt;
           return $tt;
       }
    }
?>

Note that the variable $tt was within the scope of the function generic , to access it outside this function, it would be necessary to include in the file funcoes.php the declaration global $tt;

So I do not recommend that you follow the idea of including files within functions, you will have numerous scope problems, and will be forced to declare all the variables used ...

Create a file like: includes.php and list all the files to include in it, it will be much easier, you can be sure ...

Edit: I was able to create a way to pass all variables from the scope of the function to the global automatically:

function generic($string){
    include($string);
    foreach(get_defined_vars() as $key => $value) $GLOBALS[$key] = $value;
}
    
27.08.2014 / 01:46
-1

You are not able to run the functions because your include is giving problem:

You're like this:

generic('/sistem/exemploSistema/funcoes');

It should look like this:

 generic('/sistem/exemploSistema/funcoes.php');
    
26.08.2014 / 21:25