I am trying to use OOP PHP to access my mongodb collection. Yet on the page it returns nothing, but when I look in my apache error log it states
PHP Notice: Trying to get property of non-object in main.php on line 22
Here is the code I'm using: db.php
class db {
static $db = NULL;
static function getMongoCon()
{
if (self::$db === null)
{
try {
$m = new Mongo('mongodb://{user}:{password}@{host}:{port}/{database}');
} catch (MongoConnectionException $e) {
die('Failed to connect to MongoDB '.$e->getMessage());
}
self::$db = $m;
}
else
{
return self::$db;
}
}
}
main.php
//load db files
require_once('db.php');
class test{
public function __construct() {
echo $this->output();
}
public function output() {
$con=db::getMongoCon();
$db=$con->database;
$test=$db->collection;
$n=$test->find();
print_r($n);
}
}
new test();
This all works using procedural code, and I have been able to insert data in this method as well - so it should work (I have removed the database details here for obvious security reasons).
Note: I have read this but it is still not working.