如何使外部类对象(在类外部实例化)可供 php 中的任何其他类访问


How to make an external class objects (which are instantiate outside the class) accessible to any other classes in php

# 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();

有 4 个文件,数据库.php、加密.php、通知.php和索引.php。前 3 个文件仅实例化一次。可以在任何文件或类中调用它们。如何调用或访问在类外部实例化的类的对象。例如:

$db = new Database();
$crypt = new Crypt();
$notify = new notify();

的对象是否在 index.php 中的类 1 和类 2 之外实例化。如何访问class one {}class two {}中的对象$db$crypt$notify?如何使这些对象像全局对象一样工作?

您正在寻找的是单例设计模式。在软件中引入全局状态时要小心,因为它可能会在以后引起问题。

针对您的特定问题的一种可能解决方案:

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);

请注意,"可调用"类型提示在 php5.4+ 中可用