我的PHP类中的自定义构造函数不起作用

I am trying to get acquainted to PHP and came across the problem that I'll describe now.

Basically, I have a parent class with the following structure:

abstract class DBConn {
    protected $con;

    abstract function __construct($host, $user, $pass);
    abstract function GetConnectionString($host, $user, $pass);
}

Also, I have a child class with this 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 to work. I use the 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 I receive no dump, probably because $dx is NULL.

I don't have enough experience with PHP to know what is causing this problem, nor do I have enough with nginx (the web server that I'm using) to know where to look for. Could someone please help me find the solution?

Thanks!

EDIT: I modified the code according to the suggestions, but realized the following: While my test file uses two REQUIREs, the file where the MongoDBConn class is declared uses a couple more. When I try to use "REQUIRE ...dbconn-mongo.php", the main page crashes. Is it because I can't have a REQUIRE inside a file that also uses a REQUIRE? If so, what would be a workaround? (One of the required files contains return codes; another one contains field names, and so on.)

Make your constructor public. Otherwise it doesn't get called at all, and no database connection will be created.

There are 2 ways you can do it.

First is to make __construct public. That way you can create instances of MongoDBConn.

However, since this is a DB connection and you may want it to be singleton you may want to write a getInstance method.

class MongoDBConn extends DBConn {
private $col;
private $_instance = false;
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;
}
public function getInstance($host = false, $user = false, $pass = false){
    if($this->_instance === false){
        if($host === false || $host === false || $host === false)
            throw new Exception('You must pass arguments at first MongoDBConn::GetInstance() Call');
        $_instance = new MongoDbConn($host, $user, $pass);
    }
    return $this->_instance

}

}

That way you're sure none makes 2 connections. I think the second way should be more desired by you. It's easier to get instance in classes without global words and passing MongoDBConn object as parameter. It will keep your code clean.

@Edit: added support for passing host, user and password.