tags:

views:

77

answers:

3

I've been learning OOP programming for about a year and a half now and have developed a fairly standard framework to which I generally abide by. I'd love some feedback or input on how I might improve some functionality or if there are some things I'm overlooking.

VIEW MODE

1) Essentially everything starts at the Index.php page. The first thing I do is require my "packages.php" file that is basically a config file that imports all of the classes and function lists I'll be using.

2) I have no direct communication between my index.php file and my classes, what I've done is "pretty them up" with my viewfunctions.php file which is essentially just a conduit to the classes so that in my html I can write <?php get_title('page'); ?> instead of <?php echo $pageClass->get_title('page'); ?> Plus, I can run a couple small booleans and what not in the view function script that can better tailor the output of the class.

3) Any information brought in via the database is started from it's corresponding class that has direct communication with the database class, the only class that is allowed direct to communicate with the database (allowed in the sense that I run all of my queries with custom class code).

INPUT MODE

1) Any user input is sent to my userFunctions.php.

2) My security class is then instantiated where I send whatever user input that has been posted for verification and validation.

3) If the input passes my security check, I then pass it to my DB class for input into my Database.

php general model

FEEDBACK

I'm wondering if there are any glaringly obvious pitfalls to the general structure, or ways I can improve this.

Thank you in advance for your input. I know there is real no "right" answer for this, but I imagine a couple up votes would be in order for some strong advice regarding building frameworks.

-J

+1  A: 

Well, the only issue that I can see (without seeing code), is that SQL will be everywhere. What I'd suggest is creating a "model" layer in front of the DB connection class. That way, all your sql is in one spot (break it off into multiple models, etc). It makes maintenance MUCH easier (if you want to add a column to a table, optimize a query, etc)...

Otherwise, it looks like a good start!

ircmaxell
That's very true. I have tons of query code going on in my userfunction.php and view classes. I run the actual query off of my DB class with a function that looks like $dbConnect->runQuery($query).. but the actual query string is written inside the view class (or user function script).
Jascha
I've actually really liked the view concept from Kohana... Basically, `$view = new View('path/to/template');` Then assign your variables to the view `$view->title = 'blah';`... Then, all you need to do is `echo $view;` No need for `$viewObject->get_title();` inside the template, just `echo $title;`...
ircmaxell
I'm mixed on this. I think you're very right, a "model" class is a huge benefit. But I am also a big believer in David Heinemeier Hansson's philosophy that [SQL is neither dirty nor bad](http://blogs.msdn.com/aconrad/archive/2005/12/29/508068.aspx)
Josh
@Josh - I agree with the sentiments in that article. I just prefer to keep all that SQL in one layer (typically a handful of classes) to make it easy to find what needs to be changed. It's a lot easier to look for all inserts into a particular table by opening one class, then it is grepping or trying to thumb through 40 or 50 files... Either way, you have a very valid point...
ircmaxell
+2  A: 

One thing I would recommend you do is to go ahead and separate your functions into classes. I understand the points you made about avoiding instantiations but consider this: any framework will, by necessity, begin to accumulate a large number of functions.

Instead of doing

<?php get_title('page'); ?>

you would be better served to create a Page class with all of its functions inside of said class which you call statically. Then, your code becomes

<?php Page::GetTitle('page'); ?>

a much more descriptive naming convention and will become critical later on when trying to avoid naming collisions (you only have to avoid name collisions on say, 50 classes, rather than two thousand functions).

I would study up on the Model-View-Controller design methodology (as ircmaxell hinted at in his post). A lot of very powerful and very well-written frameworks apply this principle, and not just PHP frameworks either. My suggestion - study Yii for how your application should be controlled - very slick and the creator makes excellent use of static variables to control class instantiation.

Good luck with your framework!

Jarrod
@Jarrod, thank you for your input. This is the funny gray area of PHP and OOP for me... isn't requiring a page almost the same as building a class? I mean, when you include a page, you are importing a set of functions and possibly a set of variables. There are a few differences, but if you're just looking to house functions, it seems like a hack way of building classes without actually building classes.Thanks again for the feedback!
Jascha
+1 for classes, even if they're just used for namespacing as your example.
Josh
+1  A: 

Nice presentation. I would definitely look under the hood at other popular frameworks for some insight.

At first glance, I would suggest to see if you can find a way to only load the classes you need per request. Loading them all for every request may become unfeasible if the class library grows large.

webbiedave
I was wondering about that. That wouldn't be too difficult with a couple booleans. Love it! thank you!
Jascha