Tree Class Structure
Posted: Tue Nov 03, 2009 2:26 pm
For my framework I've been wanting to try out this new methodology of class structure and I'm looking for ideas on how best to approach this problem. In the past when I've designed a class structure its been organized somewhat flatly. For example, for the framework I might have had a structure that looked like this:
Application
|---- User
|---- ACL
|---- MySQL
|---- DB2
|---- Oracle
|---- ORM
|---- Router
|---- Pages
|---- Models
L---- Controller
.... and while this has worked fine in the past I'm struggling with the concept of a persistent application that I can reference throughout the execution of the application (ie: a single page request). Previously I would simply pass any data that I needed through the function parameters as the need occasioned.
My new method involves the grouping of classes through categories - classes will exist which serve little to no purpose other than to provide a categorical namespace (not a literal PHP namespace) for pointers to children classes. So, the above tree would be reorganized thusly...
Application
|---- User
|-------- user funcs & properties
|---- Security
|---- Database
|-------- MySQL
|-------------- connect, ect. etc.
|-------- DB2
|-------------- connect, ect. etc.
|-------- Oracle
|-------------- connect, ect. etc.
|---- Page
|--------- Current
|--------- Request
The database example was the only one I really expanded out but hopefully you get the idea. So, for example, to connect to a MySQL database would be like this....
Database is its own class but is merely serving as a container for pointers to the MySQL, DB2, Oracle, and ODBC classes that are individual, standalone classes (there is no inheritance taking place anywhere here). Now, all of this works fine - my problem comes from different classes needing access to other classes for information - basically, each class needs a reference back to the original application instantiation so that the class MySQL can obtain information from the class User if it needs to.
Example. Here's a look at the basic structure of the application.
Obviously, in the connect function of MySQL I need something other than $APPLICATION_REFERENCE to get this to work. Somehow I need a reference back to the original instantiation. So does anyone have any ideas on how I could implement this or are my hopes of this class structure a hopeless pipe dream? 
Application
|---- User
|---- ACL
|---- MySQL
|---- DB2
|---- Oracle
|---- ORM
|---- Router
|---- Pages
|---- Models
L---- Controller
.... and while this has worked fine in the past I'm struggling with the concept of a persistent application that I can reference throughout the execution of the application (ie: a single page request). Previously I would simply pass any data that I needed through the function parameters as the need occasioned.
My new method involves the grouping of classes through categories - classes will exist which serve little to no purpose other than to provide a categorical namespace (not a literal PHP namespace) for pointers to children classes. So, the above tree would be reorganized thusly...
Application
|---- User
|-------- user funcs & properties
|---- Security
|---- Database
|-------- MySQL
|-------------- connect, ect. etc.
|-------- DB2
|-------------- connect, ect. etc.
|-------- Oracle
|-------------- connect, ect. etc.
|---- Page
|--------- Current
|--------- Request
The database example was the only one I really expanded out but hopefully you get the idea. So, for example, to connect to a MySQL database would be like this....
Code: Select all
<?php
$app = new Application();
$app->Database->MySQL->connect(CONNECTION_STRING);Example. Here's a look at the basic structure of the application.
Code: Select all
<?php
$app = new Application();
$app->MySQL->connect();
class Application
{
//Declare our Namespaces
public $User;
public $MySQL; //flattened the database class for the example
public function __construct()
{
$this->User = new User;
$this->User->username = "Jack";
$this->MySQL = new MySQL;
}
}
class User
{
public $username;
}
//separately.......
class MySQL
{
public function connect()
{
$username = $APPLICATION_REFERENCE->User->username;
mysql_connect(server, $username);
}
}