class inside a class..possible?
Moderator: General Moderators
class inside a class..possible?
is this possible?a class inside another class?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
class Class_One
{
public function __construct()
{
class Class_Two
{
public function __construct() {
}
}
}
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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:
Although I personally use a singleton pattern to access my database class from wherever I need.
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);- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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();(#10850)