mysql - PHP: Is implementing the database layer as a singleton acceptable? Code inside -


i know singletons bad. bad this, too?

class daomysql {      private static $instance;     private $pdo;      private function __construct() {         $this->pdo = new pdo('mysql:dbname='.mysql_default_database.';host='.mysql_hostname, mysql_username, mysql_password);         $this->pdo->query('set names \'utf8\'');     }      /**      * @return daomysql      */     static public function singleton() {         if (!isset(self::$instance)) {             $c = __class__;             self::$instance = new $c();         }         return self::$instance;     }      /**      * @return pdo      */     public function getpdo() {         return $this->pdo;     }  } 

to use this, this. (this bean class data objects extends.)

public function delete() {     $calledclassname = get_called_class();     $query = "delete `" . $calledclassname::table . "` `id` = $this->id";     return daomysql::singleton()->getpdo()->exec($query); } 

many people starting use dependency injection containers manage objects instead of using singletons. perhaps it's worth look? need ensure objects can access container. can fetch else there.

personally use sfservicecontainer symfony components. it's stand-alone di container , seems quite popular these days.

update

you don't need use framework or library. fabien potencier's articles on dependency injection should give enough grasp of di implement own. why reinvent wheel? not using good, existing library smells of nih.

note there many other di libraries besides sfservicecontainer use. note sfservicecontainer stand-alone library. not need symfony or other framework. requires plain old php.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -