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 best practices for setting cookies for an entire directory or domain in PHP?
- What potential issue could arise when using multiple IF statements in PHP to check user input?
- How can one effectively assess the quality of their PHP code and ensure they have chosen the optimal development approach?