Question

I'm still trying to get to grips with dependency injection and loosely coupled objects. Looking at the Zend Framework Quick Start, I noticed the following methods in the Application_Model_GuestbookMapper:

public function setDbTable($dbTable)
{
    if (is_string($dbTable)) {
        $dbTable = new $dbTable();
    }
    if (!$dbTable instanceof Zend_Db_Table_Abstract) {
        throw new Exception('Invalid table data gateway provided');
    }
    $this->_dbTable = $dbTable;
    return $this;
}

public function getDbTable()
{
    if (null === $this->_dbTable) {
        $this->setDbTable('Application_Model_DbTable_Guestbook');
    }
    return $this->_dbTable;
}

My question is: is this an example of tight coupling, as the Mapper depends on the DbTable? If it was to use dependency injection instead, would there be something like this in the controller?:

$guestbookMapper = Application_Model_GuestbookMapper;
$guestbookMapper->setDbTableGuestbook(new Application_Model_DbTable_Guestbook);

If so then why is Zend recommending tightly coupled objects?

Was it helpful?

Solution

My best guess is that most people who would even click a quick start guide are probably novice at best. Introducing the Database Abstraction Layer can be somewhat confusing to a newcomer, adding yet another complexity such as dependency injection might be a little too much to swallow. Especially to those new to OOP.

Also, usage of Zend_Db_Table is [need citation] the most common type of DAL in the Zend Framework community. That said, it's safe to assume (for them) that most people almost expect DbTable to be used in a quick start example because that's what everyone is going to want to know how to do.


This, however, does not mean that the Zend Framework is tightly coupled. Quite the contrary, they boast about being very loosely coupled. Some say that is in the eye of the beholder.. There is some coupling but that it's still a great framework.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top