我试图整合用户的方式能够有配置设置保存到一个空白的PHP文件是这样的:PHP - 可以将Require_Once用作对象吗?
<?php // Configuration.php
$con = array(
'host' => 'host'
'user' => 'username',
'pass' => 'password',
'name' => 'dbname'
);
?>
我曾尝试:
class Configuration{
public $database = require_once 'Configuration.php';
}
$config = new Configuration;
print_r($config->database->con);
这是可能的或不?当访问Configuration.php页面时会出现一个显示,所以我不想include
这个页面在这个网站上,只有require
它的属性。
在此先感谢。
Updated working code for viewers - 由@Yoshi使用类和构造函数的
的config.php -
if(defined('DFfdcxc58xasdGJWdfa5hDFG')): // Random unique security key
return array(
'host' => 'localhost',
'user' => 'bob',
'pass' => '123',
'name' => 'data'
);
endif;
数据库类:
interface Dashboard{
public function initialize($actual);
}
define('DFfdcxc58xasdGJWdfa5hDFG',0); // Random unique security key
class Configuration{
protected $config = require_once('Config.php');
protected $api_key = "Xc4FeSo09PxNcTTd3793XJrIiK";
}
class DashboardSettings{
public $alerts = array();
protected $comments = true;
protected $read_only = false;
protected $safe_mode = false;
}
class Database extends Configuration extends DashboardSettings implements Dashboard{
public function __construct(){
$this->db = mysqli_connect($this->config[0],$this->config[1],$this->config[2],$this->config[3]);
if(mysqli_connect_errno){ array_push($this->alerts, 'Error connecting to Database...'); $this->safe_mode = true; }
}
public function initialize($actual = null){
if($actual != null){
// Handle incomming setting - reference DashboardSettings
} else {
// Handle all settings - reference DashboardSettings
}
}
}
不,这是不可能的 –
有什么办法来实现这一点,即使是在一个单独的方法? – KDOT
加载配置并将其作为构造函数参数传递。 ('新配置($ con)') – Yoshi