我有一个php页面,其中有一些关于数据库的变量,即服务器地址,用户名和密码等。
config.php将包括
<?php
$dbserver="";
$username="";
$password="";
$database="";
?>我有一个类,其中包含了我的网站所需的所有功能。我怎样才能将我的php页面变量导入到这个类中,以便用于数据库连接?
我的班级
<?php
class a{
include("config.php");
function db_connect(){
mysql_connect($dbserver,$username,$password);
}
}
?>发布于 2010-11-13 12:31:11
我在类的构造函数中使用了数据库配置。我认为这是唯一的解决方案,不包括场景中的任何第三页。
发布于 2010-11-11 18:17:54
通常为了这个目的,常量是存在的。
但是如果你想使用变量,你所要做的就是require_once(yourFile),然后当你想在一个类的方法中使用这些变量(它们是全局的)时,只需将它们引用为global $myVar; (就像你在声明它一样)。对于要在上下文中使用的每个全局变量,只需执行一次此操作。
示例:
settings.php:
$conn = 'my connection';MyClass.php:
class MyClass
{
function DoSomething()
{
require_once('settings.php');
global $conn;
doSomethingWith($conn);
}
}发布于 2010-11-11 18:31:35
更新
对于需要配置选项的数据库类,最简单的方法是使用配置值作为参数(下面我的原始答案的示例1)。
一种更复杂但也更灵活的方法是Config-Class。
class Config
{
private $config = array();
public function __construct(array $config)
{
$this->config = $config;
}
public function Register($key, $value)
{
$this->config[$key] = $value;
}
public function Get($key)
{
if (!isset($this->config[$key])) return null;
return $this->config[$key];
}
}您的DB类将如下所示:
class Database
{
private $config = null;
public function __construct(Config $config)
{
$this->config = $config;
}
public function Connect()
{
do_connect_stuff($this->config->Get('host'), $this->config->Get('user'), .....);
}
}文件config.php
<?php
$config = new Config(
array(
"host" => "localhost",
"user" => "user",
...
)
);
/*
alternative:
$config = new Config();
$config->Register('host', 'localhost');
$config->Register('user', 'user');
...
*/
?>需要数据库的文件:
<?php
$database = new Database($config);
$database->Connect();
?>附带提示:使用PDO,它比旧的mysql_*函数要好得多。
原始答案
正确的方式应该是将变量作为参数传递给函数,或者在创建对象时传递它们。您还可以使用Init方法来传递参数。
示例:
(您应该使用以下哪些代码取决于您已有的代码以及您的代码是如何设计的,最干净的方式是在调用ProcessAction方法时为其传递变量的对象)
假设在您的脚本中有一个从$_GET或其他方式获得的变量$action。
使用Object的
class Controller
{
public function ProcessAction($action, $some_other_parameter, $and_yet_another_parameter)
{
[...]
}
}然后,您可以使用
$action = do_some_stuff_to_get_action();
$controller = new Controller();
$controller->ProcessAction($action, $other_parameter, $second_parameter);使用静态类的
class Controller
{
public static function ProcessAction($action, $some_other_parameter, $and_yet_another_parameter)
{
[...]
}
}调用时使用:
$action = do_some_stuff_to_get_action();
Controller::ProcessAction($action, $other_parameter, $second_parameter);在调用函数之前传递参数的
对象
class Controller
{
private $action = "";
private $some_other_parameter = "";
public function __construct($action, $some_other_parameter)
{
$this->action = $action;
$this->some_other_parameter = $some_other_parameter;
}
public function ProcessAction()
{
if ($this->action == 'do_stuff')
{
[...]
}
}
}调用时使用:
$action = do_some_stuff_to_get_action();
$controller = new Controller($action, $other_parameter);
$controller->ProcessAction();静态方法
class Controller
{
private static $action = "";
private static $some_other_parameter = "";
public static function Init($action, $some_other_parameter)
{
self::$action = $action;
self::$some_other_parameter = $some_other_parameter;
}
public static function ProcessAction()
{
if (self::$action == 'do_stuff')
{
[...]
}
}
}调用时使用:
$action = do_some_stuff_to_get_action();
Controller::Init($action, $other_parameter);
Controller::ProcessAction();https://stackoverflow.com/questions/4153313
复制相似问题