我想添加一個靜態方法到我的類,它連接到我的數據庫並返回一個數組數組。例如,我有一個名爲Users的表和一個名爲User的類,我希望能夠調用$ user = User :: fetch($ id)來獲取指定用戶信息的數組。所以我的問題是,何時何地應該連接到數據庫?每次我調用用於類似目的的靜態方法時,是否需要傳遞數據庫連接信息?那只是感覺不對。PHP:以靜態方法連接到數據庫
0
A
回答
0
我做的是有一個非常早期的火一個的ConnectionManager類連接到相關數據庫但應用單例模式的應用程序運行時。所以一旦運行一次,我可以在整個項目中全局訪問連接。
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\MemcachedConnector;
use Illuminate\Database\Capsule\Manager as Capsule;
use INSP\Config;
use INSP\Core;
use INSP\Di;
/**
* Class ConnectionManager
*
* @package INSP\Database
*/
class ConnectionManager
{
/**
* @var \Illuminate\Database\Connection
*/
protected static $instance;
/**
* Loads database configuration from static file if it exists if not it loads from
* Wordpress defaults
*
* @param bool $config
*/
public function __construct($config = false)
{
/*
* If config is not provided in the constructor check for a config file in the
* config directory, if that doesn't exist use the database config from wp_config
*/
if (!$config && file_exists(Core::baseDir() . '/Config/database.php')) {
$config = include(Core::baseDir() . '/Config/database.php');
} else {
if (!defined('DB_HOST')) {
if (file_exists(Core::baseDir() . '/local-config.php')) {
define('WP_LOCAL_DEV', true);
require_once(Core::baseDir() . '/local-config.php');
} else {
require_once(Core::baseDir() . '/production-config.php');
}
}
$config = array(
'driver' => 'mysql',
'host' => DB_HOST,
'database' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASSWORD,
'charset' => DB_CHARSET,
'collation' => 'utf8_unicode_ci',
);
}
return self::connect($config);
}
/**
* This method is in charge or setting up all connection's including
* the k2 and mocked testing connection(uTest)
*
* @param $config
*
* @return \Illuminate\Database\Connection
*/
public static function connect($config)
{
$capsule = new Capsule();
// Load some ancillary configurations
$k2Config = Config::getAll('Apis/k2');
$unitConfig = array(
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
);
// Add all needed configurations to connections and name them if needed
$capsule->addConnection($config);
$capsule->addConnection($k2Config, 'k2');
$capsule->addConnection($unitConfig, 'uTest');
// Add capsule to global namespace so we can use it later
$capsule->setAsGlobal();
// Start the caching library so we can use the remember(ttl) method in our queries
$container = $capsule->getContainer();
$cacheConfig = Config::getAll('cache');
$memcachedServers = Config::get('cache.memcached');
$container['memcached.connector'] = new MemcachedConnector();
$container['config']['cache.driver'] = $cacheConfig['driver'];
$container['config']['cache.path'] = $cacheConfig['file']['directory'];
$container['config']['cache.connection'] = null;
$container['config']['cache.table'] = 'cache';
$container['config']['cache.memcached'] = $memcachedServers;
$container['config']['cache.prefix'] = $cacheConfig['prefix'];
// If Memcached is not installed default to file storage
if (!class_exists('Memcached', false)) {
$container['config']['cache.driver'] = 'file';
}
// Start Dependency Injection if it hasn't already been started
Di::init();
$cacheManager = new CacheManager($container);
Di::set('cacheManager', $cacheManager);
$capsule->setCacheManager($cacheManager);
return $capsule->connection();
}
/**
* @param bool $config
*
* @return ConnectionManager
*/
public static function init($config = false)
{
if (!is_object(self::$instance)) {
self::$instance = new ConnectionManager($config);
}
return self::$instance;
}
}
這是我的連接管理器,它使用框架外部的數據庫類。一旦這樣跑我還有一個門面即字面
use Illuminate\Database\Capsule\Manager;
/**
* Class DB
* This is a Facade of the Connection established by ConnectionManager
*
* @package INSP\Database
*/
class DB extends Manager
{
}
,它可以讓我用DB::table()
.......代替Manager::table()
。
希望這會有幫助
0
你需要你建立連接的第一次,你需要它,並關閉連接,當應用程序被關閉(__destruct
方法或register_shutdown
)
相關問題
- 1. 數據庫連接用靜態方法
- 2. 使用HttpContext和數據庫連接的靜態方法
- 3. 在Java中與靜態方法共享數據庫連接
- 4. java:使用靜態方法獲取數據庫連接
- 5. PHP:如何將現有的數據庫連接傳遞給靜態類方法?
- 6. PHP - 無法連接到MySQL數據庫
- 7. 無法連接到數據庫與PHP
- 8. PHP無法連接到MySQL數據庫
- 9. PHP 7無法連接到數據庫
- 10. 無法連接到數據庫在php
- 11. 無法連接到MySQL數據庫 - PHP
- 12. 連接到數據庫php
- 13. 連接到數據庫,PHP
- 14. 連接到數據庫,PHP
- 15. PHP - 靜態數據庫連接類的良好設計模式
- 16. PHP&PDO:可以連接到數據庫;似乎無法查詢
- 17. 數據庫連接錯誤(3):無法連接到數據庫',
- 18. 靜態數據庫連接到我的Android應用
- 19. UNIX:靜態庫鏈接到靜態庫
- 20. 鏈接第三方靜態庫到另一個靜態庫
- 21. PHP靜態方法
- 22. 如何動態連接到數據庫
- 23. 動態連接到多個數據庫
- 24. 我無法連接到MySQL數據庫寧靜
- 25. 無法使用php連接數據庫
- 26. PHP「數據庫無法連接」MAMP
- 27. PHP無法與Oracle數據庫連接
- 28. 非靜態方法DB ::連接()不應該叫靜態
- 29. 靜態數據庫連接作爲函數的參數
- 30. 動態數據庫連接
不,你不需要每次都傳遞數據庫連接信息。嘗試數據庫連接的單例模式。 –