自己简单设计了PHP MVC框架,我想控制器或模型全局调用config里的数组。
框架:
app
|-controllers
|-homcontrollers.php
|-models
config
|-config.php
index.php
在index.php文件已经加载config.php文件了
require 'config/config.php'
那么问题来了,在homcontrollers.php如何自动获取config.php文件?
我不想homcontrollers.php 都这样加载:
class homcontrollers
{
public function index()
{
require '../config/config.php'
}
public function about()
{
require '../config/config.php'
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
实现一个Config的类,以下是伪代码:
Arr.php
为了图方便,我就直接用laravel内部的数组类了
https://github.com/laravel/framework/blob/5.3/src/Illuminate/Support/Arr.phpconfig/app.php
Cache.php
使用
你可以尝试用autoload
在index.php 获取了不是后面的controller都能自动require了吗?这个index.php不是启动文件?
获取内容可以 $config = include "config/config.php";
var_dump($config);
设置一个管理配置文件的类或者接口,再把该类设置成自动加载即可!实现的方法有很多,关键怎么处理,看你的喜好了!
在index.php入口执行的时候 config.php已经包含进来的,所有的常量已经初始化好了.