在项目中重构 require_once 文件 [英] Refactoring require_once file in a project

查看:28
本文介绍了在项目中重构 require_once 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PHP 初学者.我正在使用此目录层次结构进行项目:modelcontrolviewhelper 文件夹位于我的项目文件夹

I'm a beginner in PHP. And i'm working on project with this directories hierarchy : model, control, view and helper folders are in my project folder

现在我正在尝试在 controlmodelinit.phprequire_oncecode> 文件,这是我的 init.php

Now i'm trying to write a file init.php and require_once it in each of control and model files, here's my init.php

<?php

    $current_dir = basename(getcwd());
    $model_dir = "model";
    $helper_dir = "helper";

    function require_helper(){
        $handle = opendir("../{$helper_dir}");
        while($file = readdir($handle)){
        if($file != "." && $file != ".."){
                require_once "../{$helper_dir}/{$file}";
            }
        }
    }

    if($current_dir == "control"){
        $handle = opendir("../{$model_dir}");
        while($file = readdir($handle)){
            if($file != "." && $file != ".."){
                require_once "../{$model_dir}/{$file}";
            }
        }

        require_helper();

    } elseif( $current_dir == "model") {
        $handle = opendir($current_dir);
        while($file = readdir($handle)){
            if($file != "." && $file != ".."){
                require_once "{$file}";
            }
        }

        require_helper();
    } 
?>

但是当我测试我的项目时出现此错误:

But when i test my project i get this error :

注意:未定义变量:session in C:\wamp\www\harmony\control\login.php 第 11 行

Notice: Undefined variable: session in C:\wamp\www\harmony\control\login.php on line 11

这是我的 login.php 文件:

<?php
    require_once "../helper/init.php";
?>

<?php

    if(isset($_GET["logout"]) && $_GET["logout"] == "true" ){
        $session->logout();
    }

    if($session->is_logged_in()){
        redirect_to("../view/index.php"); 
    }

    if(isset($_POST["submit"])){
        $username = $db->escape_value($_POST["username"]);
        $password = $db->escape_value($_POST["password"]);
        $password = hash('sha1' , $password);
        $arr = User::auth($username , $password);
        if($arr){
            $usr = $db->instantiate($arr);            
            $session->login($usr);
        } else {
            Session::notify("Invalid login information.");
        }
    }    

?>

你能帮我吗?怎么了?

推荐答案

您正在尝试访问函数内部的 $current_dir、$model_dir 和 $helper_dir.您不能访问在函数外部声明的变量,除非它们被声明为全局变量,否则实际上传递给函数.

You're trying to access $current_dir, $model_dir and $helper_dir inside of functions. You can't access variables which were declared outside of a function unless they are declared global, or else actually passed into the function.

例如:

function require_helper(){
    global $helper_dir;//this is key
    $handle = opendir("../{$helper_dir}");
    while($file = readdir($handle)){
    if($file != "." && $file != ".."){
            require_once "../{$helper_dir}/{$file}";
        }
    }
}

这篇关于在项目中重构 require_once 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆