Is it best practice to use Zend_Registry to make the DB adapter globally available in ZendFramework?
Using Zend_Registry to make the DB adapter globally available in Zend Framework is not considered a best practice. It is recommended to use Dependency Injection instead, as it promotes better code organization, testability, and flexibility.
// Using Dependency Injection to pass the DB adapter to classes that need it
class SomeClass
{
protected $dbAdapter;
public function __construct(\Zend\Db\Adapter\Adapter $dbAdapter)
{
$this->dbAdapter = $dbAdapter;
}
public function someMethod()
{
// Use $this->dbAdapter to interact with the database
}
}
// In your application bootstrap or configuration
$dbAdapter = new \Zend\Db\Adapter\Adapter($dbConfig);
$someClass = new SomeClass($dbAdapter);
Related Questions
- What are the potential issues with using mod_rewrite to hide PHP variables in URLs?
- What are the different ways to store time durations in a PHP MySQL database and what are the pros and cons of each method?
- What are the factors that determine the success of file downloads in PHP based on browser settings?