如何使外部类对象(在类外部实例化)可供php中的任何其他类访问
有4个文件,database.php、encryption.php、notify.php和index.php。前3个文件仅实例化一次。它们可以在任何文件或类中调用。如何调用或访问在类外部实例化的类的对象。例如:如何使外部类对象(在类外部实例化)可供php中的任何其他类访问,php,class,object,global-variables,instantiation,Php,Class,Object,Global Variables,Instantiation,有4个文件,database.php、encryption.php、notify.php和index.php。前3个文件仅实例化一次。它们可以在任何文件或类中调用。如何调用或访问在类外部实例化的类的对象。例如: # database.php # class Database { public function connect(){ //code } public function select(){ //code } public function insert(){ /
# database.php #
class Database
{
public function connect(){ //code }
public function select(){ //code }
public function insert(){ //code }
public function update(){ //code }
public function delete(){ //code }
}
# encryption.php #
class Crypt
{
public function encrypt(){ //code }
public function decrypt(){ //code }
}
# notify.php #
class notify
{
public function setNotify(){ //code }
public function getNotify(){ //code }
}
# index.php #
include ('database.php');
include ('encryption.php');
include ('notify.php');
$db = new Database();
$crypt = new Crypt();
$notify = new notify();
class one
{
function execute()
{
$db->select(); // $db is the external object of Database class (database.php)
$notify->setNotify(); // $notify is the external object of Notify class (notify.php)
}
function store()
{
$db->insert(); // $db is the external object of Database class (database.php)
$notify->setNotify(); // $notify is the external object of Notify class (notify.php)
}
}
class two
{
function cypher()
{
$crypt->encrypt(); // $crypt is the external object of Crypt class (crypt.php)
$db->update(); // $db is the external object of Database class (database.php)
$notify->setNotify(); // $notify is the external object of Notify class (notify.php)
}
}
$one = new one();
$two = new two();
$one->execute();
$two->cypher();
$one->store();
是在index.php中的类1和类2之外实例化的对象。如何访问
一类{}
和二类{}
中的对象$db
、$crypt
和$notify
?如何使这些对象像一个全局对象一样工作?您要寻找的是一个全局对象。在软件中引入全局状态时要小心,因为它以后可能会导致问题
针对您的特定问题的一种可能的解决方案:
$db = new Database();
$crypt = new Crypt();
$notify = new notify();
请注意,php5.4+中提供了“可调用”类型提示,这可以通过依赖项注入来完成。你可以找到几个例子。
class SingletonManager
{
/**
* Stores singleton instances
* @var array
*/
protected static $_instances = array();
/**
* Used to instantiate singletons
* @param callable $factoryMethod
* @return object of type returned by $factoryMethod or null
*/
public static function factory(callable $factoryMethod)
{
/**
* @var object
*/
$singleton = $factoryMethod();
// save reference to created object
self::$_instances[get_class($singleton)] = $singleton;
// return object
return $singleton;
}
/**
* Returns singleton object
* @param string $className
* @return object|null
*/
public static function instance($className)
{
// check if there is an instance of $className
if (isset(self::$_instances[$className])) {
return self::$_instances[$className];
}
return null;
}
}
// end class definition
// mock database class
class Database
{
public $username = '';
}
// create database object for the first time
SingletonManager::factory(function(){
$database = new Database();
$database->username = "username";
return $database;
});
// access database object later from anywhere
$databaseObject = SingletonManager::instance("Database");
print_r($databaseObject);