在OOP PHP中难以掌握不同级别的变量。我希望有一个连接文件,可以从所有的类和函数通过我的项目访问。我包括'config.php‘,但是找不到$mysql变量。任何帮助都非常感谢;
我有个文件
config.php
public $mysqli = new mysqli('localhost', 'usernameEG', 'passwordEG', 'databaseEG');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}还有几个类文件
class.User.php
<?php
class User {
private $mysqli = null;
function __construct(){
//=========
//include config.php here?
}
function myProfile($Id){
$stmt = $this->mysqli->prepare("SELECT First_Name, Last_Name, Email, DateJoined FROM members WHERE Id = ? ");
$stmt->bind_param('i',$Id);
$stmt->execute();
$stmt->bind_result($fname,$lname,$email,$date);
$stmt->store_result();
$stmt->fetch();
echo "<p>User#: ".$Id."</p>";
echo "<p>First Name: ".$fname."</p>";
echo "<p>Last Name: ".$lname."</p>";
echo "<p>Email: ".$email."</p>";
echo "<p>Date Joined: ".$date."</p>";
$stmt->close();
}
function example(){
EXAMPLE CODE HERE WILL ALSO NEED TO BE ABLE TO USE SAME CONNECTION
}
}发布于 2014-03-20 17:58:49
所以,不要像在另一个答案中暗示的那样,采取一种单独的方法。让我给你提供一种依赖注入方法。对于许多经验丰富的开发人员来说,依赖注入是一种首选方法,因为它允许接收依赖关系的类与依赖关系有更松散的耦合(例如,您甚至不需要知道类的名称来实例化依赖关系,就像使用单例方法一样)。您只需要知道所传递的依赖项满足了某些接口需求(即它实现了某种接口或某种类型的类)。
所以看起来更像这样:
class User {
protected $mysqli = null;
// other properties
public function __construct(mysqli $mysqli) {
$this->mysqli = $mysqli;
// other constructor operations
}
// other methods
}这是怎么回事?在这里,您强制执行在实例化该类时传递类型为mysqli的对象的要求。这可能是一个mysqli对象,甚至可能是您自己的扩展mysqli的自定义类,User类并不关心,只要实现了所有mysqli类方法
用法可能就像
require('/path/to/db_config.php'); // an include which creates a mysqli object or provides a factory for one, or whatever
$user = new User($mysqli);
$user->foo(); // do something有趣的是,有时您可能会看到使用单子模式以及依赖注入。因此,假设您有一个具有单例功能的mysqli_singleton_factory类来为您提供单个实例化的mysqli对象。其用法可能如下所示:
require('/path/to/db_config.php'); // an include which provides a singleton
$user = new User(mysqli_singleton_factory::get_instance());
$user->foo(); // do something
$other_class = new other_class_requiring_mysqli(mysqli_singleton_factory::get_instance());
$other_class->bar(); // do something在这里,您已经保证在脚本执行期间只有一个实例化的mysqli对象,并且您已经将User类与对象本身的任何实例化分离开来。
作为参考,单例模式可能如下所示:
class mysqli_singleton_factory {
protected static $mysqli = null;
protected function __construct() {
// assume DB connection setting have been pre-defined as constants somewhere
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// make sure connection worked
if($mysqli->connect_error) {
throw new Exception(__METHOD__ . ' at line ' . __LINE__ . ' failed with: ' . $mysqli->connect_error);
}
self::mysqli = $mysqli;
}
public static function get_instance() {
if (false === self::mysqli instanceof mysqli) {
self::__construct();
}
return self::mysqli;
}
}发布于 2014-03-20 17:35:59
我以前已经做过很多次了,我发现很容易实现“类单例”类作为数据库连接器,然后应用程序中的任何对象都可以引用它。
config.php
Config.php是设置凭据和实际构造数据库类的地方。
$dbHost = 'localhost';
$dbUser = 'someUser';
$dbPass = 'somePass';
$dbSchema = 'someSchema';
Database::$db = new MySQLi($dbHost, $dbUser, $dbPass, $dbSchema);classes.php
classes.php是定义类的地方,它与代码的其余部分保持独立。
class Database {
public static $db;
}
class User {
...
function example()
{
$stmt = Database::$db->prepare('SELECT TRUE');
...
}
}index.php
index.php是应用程序的入口点,用于处理请求,并使用提供的类显示所需的行为。
require_once('classes.php');
require_once('config.php');
/* Application Goes Here */这确保了我的项目中的所有对象都使用相同的连接,我只需要调用一次,而且我不必过多地处理范围。
https://stackoverflow.com/questions/22540772
复制相似问题