简体   繁体   中英

Zend_db_table is there a better way?

So this is how my startup model looks like:

<?php
class Test extends Zend_Db_Table{
protected $_name = 'test';
/**
 *
 * @staticvar Test $instance
 * @return Test
 */
static public function getInstance() {
    static $instance;
    if (!($instance instanceof Test)) {
        $instance = new Test();
        $instance->init();
    }
    return $instance;
}

}

So everytime I want to use it in my controller I will have to create it $var = Test::getInstance(); is there a way to extend Zend_Controller to automate this? I mean I don't want to do this everytime in my controller, I just want to use $var->foo So can I instantiate in a plugin or so that extends Zend_Controller? Or has someone a better idea?

Maybe you have a good reason to extend your class from Zend_Db_Table instead of Zend_Db_Table_Abstract , but the latter is usually the most common way of doing it. And it doesn't force you to call getInstance() , you can just pass the DB adapter to it as a parameter to the constructor.

You can find more information here: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.defining

Hope that helps,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM