我使用常见的配置文件来管理和前端模板现在我想包含它在函数文件中如何包含它一次并在所有文件中使用它。php类的常见配置文件,并在功能中使用它们
class frontproduct{
function fetchrange(){
include('..config.php');
}
}
我使用常见的配置文件来管理和前端模板现在我想包含它在函数文件中如何包含它一次并在所有文件中使用它。php类的常见配置文件,并在功能中使用它们
class frontproduct{
function fetchrange(){
include('..config.php');
}
}
// myfile.php
include('../config.php');
class frontproduct {
function fetchrange(){
}
}
您应该包括类代码之前的配置文件,请确保你了解你应该如何使用相对路径。
试试这个
class frontproduct{
function fetchrange(){
ob_start();
include('..config.php');
$val = ob_get_clean();
return $val;
}
}
这里是在最好以最坏的做法
列表1:包括并通过构造函数注入类
include("config.inc.php");
$fp = new frontproduct($config);
2:包括并通过setter方法注入(“可选依赖性”方法)
include("config.inc.php");
$fp = new frontproduct();
$fp->setConfig($config);
3:通入函数调用(即 “不应该对象更容易” 的方法)
include("config.inc.php");
$fp = new frontproduct();
$fp->doSomething($config, $arg);
$fp->doSomethingElse($config, $arg1, $arg2);
4:进口类(又名 “沉默的依赖性方法”)
class frontproduct{
public function __construct(){
include('config.inc.php');
$this->config = $config;
}
}
5:静态属性赋值(又名“至少它不是一个全球性的”方法)
include ("config.inc.php");
frontproduct::setConfig($config);
6:全球分配(又名“是什么范围”的方法)
include ("config.inc.php");
class frontproduct{
public function doSomething(){
global $config;
}
}