我是PHP的初学者。我正在使用此目录层次结构处理项目:model
,control
,view
和helper
文件夹位于我的项目文件夹中
现在我正在尝试在每个init.php
和require_once
文件中写一个文件control
和model
,这是我的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();
}
?>
但是当我测试我的项目时,我得到了这个错误:
注意:未定义的变量:第11行的C:\ wamp \ www \ harmony \ control \ login.php中的会话
这是我的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.");
}
}
?>
你能帮帮我吗?发生什么事了?
答案 0 :(得分:1)
您正在尝试在函数内访问$ current_dir,$ model_dir和$ helper_dir。您不能访问在函数外部声明的变量,除非它们被声明为全局变量,否则实际传递给函数。
所以例如:
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}";
}
}
}