Problem with Array Variables within an Include PHP

3

I am developing a master file that includes all the files (Classes, functions, etc.).

The automatic inclusion is done as follows:

$dir = "assets/php";

$funciones = [];
function includes ($dir){
    if ($gestor = opendir($dir)) {
        while (false !== ($entrada = readdir($gestor))) {
            if ($entrada != "." && $entrada != "..") {
                if(is_dir($dir.'/'.$entrada)){
                    includes ($dir.'/'.$entrada);
                }else{

                    include_once ($dir.'/'.$entrada);
                }

            }
        }
        closedir($gestor);
    }
    }
    includes ($dir);
    var_dump($funciones);

With that I include all the files that are inside that directory and subdirectories.

In the first file I store an array with some parameters. file1.php :

    $funciones[count($funciones)] = [
    'Function_Name' =>  'WS_funcion1',
    'Operation'     =>  'fn_Datos',
    'Require_Name'  =>  'Datos_Require',
    'Require_Data'  =>  [[ 'name' => 'dato1', 'type' => 'string']],
    'Response_Name' =>  'Datos_Response',
    'Response_Data' =>  [
                            [   'name' => 'datosArray',
                                'type' => 'array',
                                'data' =>   [
                                                [
                                                    'name' => 'Datos',
                                                    'data'=> [
                                                        [
                                                            'name' => 'DatoRespuesta1','type' => 'string'
                                                        ],
                                                        [
                                                            'name' => 'DatoRespuesta2','type' => 'string'
                                                        ],
                                                        [
                                                            'name' => 'DatoRespuesta3','type' => 'int'
                                                        ]
                                                    ]
                                                ]
                                            ]
                            ]
                        ]
];

When I do the var_dump ($ functions); I simply return array(0) { } .

If I include the file manually include 'assets/php/subdirectory1/archivo1.php'; If it works, but I want to clarify that the inclusion is done since I tried it with a simple string variable and that if I still keep the value.

    
asked by Andrés 24.11.2017 в 14:06
source

1 answer

2

The problem is that the variable $funciones is not defined within the function since it is in the global scope.

You can use global in the function to solve the problem:

function includes( $dir ) {

  global $funciones;

  // Resto de código
}
    
answered by 24.11.2017 / 14:30
source