Page 1 of 1

class inside a class..possible?

Posted: Sat Apr 15, 2006 11:09 am
by pleigh
is this possible?a class inside another class?

Posted: Sat Apr 15, 2006 11:11 am
by feyd
What do you mean specifically?

Posted: Sat Apr 15, 2006 11:15 am
by John Cartwright

Code: Select all

class Class_One 
	{
		public function __construct() 
		{
			class Class_Two 
			{ 
				public function __construct() {
				
				}
			}
		}
	}
Why don't you try it yourself?

Posted: Sat Apr 15, 2006 11:16 am
by pleigh
sorry for not being brief...if i have a Page class and another class for database query, can i include the database class inside the Page class??

sorry for the lame example.. :oops:

Posted: Sat Apr 15, 2006 11:22 am
by John Cartwright
I don't know if your after the EXTENDS keyword or not..

if you don't want to extend to page class to the database class, you can either pass the database object to the page class.. as in:

Code: Select all

$database = new DatabaseQueryClass();

$pageController = new PageController(&$database);
Although I personally use a singleton pattern to access my database class from wherever I need.

Posted: Sat Apr 15, 2006 11:30 am
by pleigh
u mean like require_once('/dbconnection.php') in every page?

Posted: Sat Apr 15, 2006 12:10 pm
by Christopher
What you probably want is composition, like:

Code: Select all

class Page {
        private $db;

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

	public function render() {
                $result = $this->db->query($sql);
	}
}

$db = new Database($dsn);
$page = new Page($db);
$output = $page->render();