mongodb - Custom constructor in my PHP class doesn't work -
i trying acquainted php , came across problem i'll describe now.
basically, have parent class following structure:
abstract class dbconn { protected $con; abstract function __construct($host, $user, $pass); abstract function getconnectionstring($host, $user, $pass); }
also, have child class code:
class mongodbconn extends dbconn { private $col; private function __construct($host, $user, $pass) { try { $server = self::getconnectionstring($host, $user, $pass); $this->con = (new mongoclient($server))->selectdb('sicom'); } catch (mongoconnectionexception $ex) { $em = 'connection error'; throw new exception($em, err_db_conn, $ex); } } public function getconnectionstring($host, $user, $pass) { return "mongodb://" . $user . ":" . $pass . "@" . $host; } }
this doesn't seem work. use following php test code:
// contains class mongodbconn require $_server['document_root'] . "/knl/db/dbconn-mongo.php"; // contains sicom_* values require $_server['document_root'] . "/knl/config.db.php"; $dx = new mongodbconn(sicom_db_host,sicom_db_user,sicom_db_password); var_dump($dx);
and receive no dump, because $dx null.
i don't have enough experience php know causing problem, nor have enough nginx (the web server i'm using) know for. please me find solution?
thanks!
edit: modified code according suggestions, realized following: while test file uses 2 requires, file mongodbconn class declared uses couple more. when try use "require ...dbconn-mongo.php", main page crashes. because can't have require inside file uses require? if so, workaround? (one of required files contains return codes; 1 contains field names, , on.)
make constructor public. otherwise doesn't called @ all, , no database connection created.
Comments
Post a Comment