Git+Coffee

Creating Java-Like Nested Classes in PHP

So a buddy of mine and I were trying to figure out how to make our PHP class for connecting to our MongoDB do something like:

<?
$database = new Database();
$database->posts->add("Hello world!");
?>

and we found out that you can’t nest classes with PHP like you can in some other programming languages. Our solution was to create a class with public variables that would hold the other classes.

Example:

<?
class Database {
	private $db;
	public $posts;

	public function __construct() {
		$mongo = new Mongo();
		$this->db = $mongo->XD6;
		$this->posts = new Posts($this->db);
	}
}
?>

Therefore now we can do something in the posts class like:

<?
class Posts{
	private $db;

	public function __construct($db) {
		$this->db = $db;
	}

	public function add($title) {
		$posts->insert(array("title" => $title));
	}
}
?>

and then be able to call it in a nicer fashion like this:

<?
$database = new Database();
$database->posts->add("Hello World!");
?>

Rather than something like this:

<?
$database = new Database();
$database->addPost("Hello World!");
?>

This tactic for nesting PHP classes really helped clean up a messy garble of code and provide a more readable format than what we were using previously.