tags:

views:

338

answers:

2

I'm building my personal CMS and need your advice organizing core functions and classes. I have folders:

/shared
   /classes
       DB.php
       Session.php
       Config.php
       ...
   /libraries
       Arrays.php
       DateTime.php
       Email.php
       ...

The files in 'classes' folder contains core classes (one class per file).

Now the question is about /libraries/ - they contain functions that may be used in any class, and sometimes they use some other function from another library, and sometimes they need to access some core class. Should i just write them as functions, or make them static and wrap in some class? Basically i need them everywhere, but keep them organized.

+1  A: 

Should i just write them as functions, or make them static and wrap in some class? Basically i need them everywhere, but keep them organized.

Have you looked into namespaces yet? Wrapping all of your functions into a namespace would probably be your best bet.

Evan Fosmark
A: 

My suggestion is to first think through about the architectural goals of your CMS. Sure, it's going to be 100% yours, but that doesn't mean you wont suffer from not begin sure whats hooked where and how, what this does and how do I get that from here.

That's why I definitely wouldn't suggest you have libraries calling libraries. From my point of view, none of your classes should be dependent on anything else except for the first few core classes in your application flow since you'd like to distributed the work in some other single purposed self-contained classes. You should aim for singularity and atomicity with your core classes.

I don't know what your architectural pattern will be (I'll presume it's going to be MVC, HMVC or PAC), but I think it's best you first define a few core classes [/core] that will lay the foundations on initializing the application by instancing some libraries [/libraries] that are required for parsing the incoming requests request and doing some default tasks before initializing the requested controller [/controllers].

Libraries should have a single purpose. The session handling library should solely handle sessions, the routing library routing, etc. Initially you can create your base controller and base model and put it in [/core] and have controllers [/controllers] and models [/models] extend your base controller and model from [/core].

As always, the less coupled your components are the better. A good solution will be lightweight, small and extensive in it's purpose. It helps if you change any design ideas in the future that you can simply change the core classes and have a huge impact on the whole application without having to make any further changes in other places.

kRON
thank you for such descriptive answer
Deniss Kozlovs