Tree Class Structure

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Tree Class Structure

Post by JNettles »

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....

Code: Select all

<?php
$app = new Application();
$app->Database->MySQL->connect(CONNECTION_STRING);
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.

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);
    }
}
 
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? :wink:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Tree Class Structure

Post by josh »

My classes are organized:

Namespace_Module_[Layer_]Name

or Namespace_Module_[Layer_]Name_InheritsFrom

for example.

Foo_Catalog_Cart

In app/code/Catalog/Cart.php

or Foo_View_Helper_Foobar

My Layers are Model, View, Controller (controllers), Grid, Form, etc...

The Model layer gets no layer name in its class name though, so for insance "Form" could be viewed as it's own layer because it is a cross cutting concept, or you could view as it as just a sub-package that re-occurs in the model layer.
Post Reply