include_once has no access to properties, variables or functions of the included file

Asked

Viewed 687 times

-1

I’m creating a generic file where several systems can use the same libraries, thus generating a much smaller package. In this generic file there is a function where I do the necessary file includes. The problem that is occurring, is that when I do the system includes in that function, I do not have access to the properties, variables and functions of the included file. I will try to exemplify with the system structure.

The structure of the system is:

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

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

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

Filing cabinet funcoes.php:

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

Filing cabinet funcaoGenerica.php:

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

Filing cabinet index.php:

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

In the archive index.php I can’t access the function teste of the archive funcoes.php. Would you have some solution to that problem or some other way to it?

  • 1

    exchange the include_once by a required_once and see if there are any errors.

  • Shows any error message? Swap <? for <?php

  • no error appears, only turns me empty!

  • It works if you put include_once('/sistem/exemploSistema/funcoes.php'); instead of generic('/sistem/exemploSistema/funcoes.php');?

  • You changed what I said?

4 answers

2

0

In a normal function the include or the require has variable scope, that is, the file is only available within the scope of the function that calls it.

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 that 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.

http://php.net/manual/en/function.include.php

For your code to work it should be like this:

File functions.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.

0

You have to imagine that the include, nothing else does that include the contents of that file in that location, so if you are within a function, you will be under its scope.

For example, if you have:

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

And the file php functions.:

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

IS exactly the same thing as:

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

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


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

Create a file like: includes.php and relate on it all the files to be included, it will be much easier, you can be sure...


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

function generic($string){
    include($string);
    foreach(get_defined_vars() as $key => $value) $GLOBALS[$key] = $value;
}

-1

You are not managing to perform the functions as your include is giving problem:

It’s like this:

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

It should be like this:

 generic('/sistem/exemploSistema/funcoes.php');
  • no man, I was trying to exemplify my mistake. This code I have now invented following the same structure and the same logic of the system! There are no syntax errors, but logic errors.

  • I replicated what you did and it’s working

  • I have changed what is not working in my question

  • 1

    This response corresponds to first version of the question, where there was a typo. That’s why you can’t put pseudo-code in a question like this, it has to be the real code, @Douglasbernardino.

  • this is one of the biggest problems want to put any code and have the expected result ... whether help needs to pass as much information as possible

Browser other questions tagged

You are not signed in. Login or sign up in order to post.