Alex
Alex

Reputation: 9265

associative array foreach loop to load required files

I am trying to load all my files in 'config.php' using an associative array. the different values denote where the file can be found. However I keep getting an error output of:

Could not load: array.php, array.php, array.php, array.php, dir.php, dir.php, dir.php, dir.php, auth.php, auth.php, auth.php, auth.php, forms.php, forms.php, forms.php, forms.php, image.php, image.php, image.php, image.php, navigation.php, navigation.php, navigation.php, navigation.php, stats.php, stats.php, stats.php, stats.php, tables.php, tables.php, tables.php, tables.php, text.php, text.php, text.php, text.php, user.php, user.php, user.php, user.php, youtube.php, youtube.php, youtube.php, youtube.php, messages.php, messages.php, messages.php, messages.php, nemesis.php, nemesis.php, nemesis.php, nemesis.php, upload.php, upload.php, upload.php, upload.php, construct.php, construct.php, construct.php, construct.php, prepared_arrays.php, prepared_arrays.php, prepared_arrays.php, prepared_arrays.php

I am not sure why this is happening, nor why it states the file could not load 4 times.

<?php
    $require = array(
        'array.php' => 'f',
        'dir.php' => 'f',
        'auth.php' => 'f',
        'forms.php' => 'f',
        'image.php' => 'f',
        'navigation.php' => 'f',
        'stats.php' => 'f',
        'tables.php' => 'f',
        'text.php' => 'f',
        'user.php' => 'f',
        'youtube.php' => 'f',
        'messages.php' => 'c',
        'nemesis.php' => 'c',
        'upload.php' => 'c',
        'construct.php' => 't',
        'prepared_arrays.php' => 'l'
    );

    load($require);

    function load($require) {
        foreach ($require as $filename => $directory) {
            // init functions
            if ($directory = 'f') {
                $prep = FUNCTIONS_DIR . $filename;
                if (is_file($prep) && is_readable($prep)) {
                    require($prep);
                    $pass[] = $filename;
                } else {
                    $fail[] = $filename;
                }
            }
            // init classes
            if ($directory = 'c') {
                $prep = CLASSES_DIR . $filename;
                if (is_file($prep) && is_readable($prep)) {
                    require($prep);
                    $pass[] = $filename;
                } else {
                    $fail[] = $filename;
                }
            }
            // init templates
            if ($directory = 't') {
                $prep = TEMPLATES_DIR . $filename;
                if (is_file($prep) && is_readable($prep)) {
                    require($prep);
                    $pass[] = $filename;
                } else {
                    $fail[] = $filename;
                }
            }
            // init libs
            if ($directory = 'l') {
                $prep = LIBS_DIR . $filename;
                if (is_file($prep) && is_readable($prep)) {
                    require($prep);
                    $pass[] = $filename;
                } else {
                    $fail[] = $filename;
                }
            }
        }
        if (!empty($fail)) {
            if (!empty($pass)) {
                $passed  = implode(', ', $pass);
                $message = "Loaded: {$passed}";
                echo $message;
            }
            if (!empty($fail)) {
                $failure = implode(', ', $fail);
                $message = "Could not load: {$failure}";
                echo $message;
            }
            exit();
        }
    }
    ?>

Upvotes: 1

Views: 147

Answers (1)

CodeAngry
CodeAngry

Reputation: 12995

$directory = 'l' should be $directory == 'l' :)

PS: Same applies to all your comparisons. They are comparisons, not assignments.

Upvotes: 4

Related Questions