我对面向对象编程相当陌生。我制作了这个连接到 mysql 数据库的类,以便从模型中调用。有什么办法可以在 index.php 中包含“database.class.php”(我的 db 类文件),使其成为全局,然后像这样从任何对象访问它
$object = new object;
$object->dofunc();
另一个问题是 dofunc() 需要一个数组作为参数,我如何使这个数组也是全局的,以便可以从任何地方访问它!
这是我的数据库类
<?php
class Database {
private $db;
public function connect($config) {
if (is_array($config)) {
extract($config);
$db = mysqli_connect($host, $username, $password);
if ($db) {
echo "balbabla";
if (mysqli_select_db($db, $database)) {
}
else {
throw new exception("<br/><strong>Could not connect to $database under $host</strong>");
}
}
else {
throw new exception("<br/><strong>Could not connect to mySQL database! Please check your details</stromg>");
}
}
}
}
?>
这也是包含数组的文件
<?php
//Configuration for the MVC Framework
$_SETTINGS = array();
//Routing settings!
//Default controller(This controller will be loaded if there is none mentioned in the URI)
$_SETTINGS['default_controller'] = 'User';
//Default method(This will be the default method run if no method is mentioned in the URI)
$_SETTINGS['default_method'] = 'Register';
//Database settings
$DB_SETTINGS['host'] = 'localhost';
$DB_SETTINGS['username'] = 'root';
$DB_SETTINGS['password'] = 'foobar';
$DB_SETTINGS['database'] = 'freelance';
?>
提前致谢