重构项目中的require_once文件
我是 PHP 初学者。我正在使用此目录层次结构进行项目: model
、control
、view
和 helper
文件夹位于我的项目文件夹
现在我尝试在每个 control
和 modelinit.php
和 require_once
它code> 文件,这是我的 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.");
}
}
?>
那么您能帮我吗?出了什么问题?
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
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 :
Notice: Undefined variable: session in C:\wamp\www\harmony\control\login.php on line 11
Here's my login.php
file :
<?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.");
}
}
?>
So could you help me please ? what's wrong is going on ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试访问函数内部的 $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.
so for example: