The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Best Architexture for Blog Design?

Hi,

I'm looking for some opinions of experienced PHP coders.
I have the following situation. In order to learn PHP I decided to code my own Blog software, without any frame work or anything. Every Single line is written by me. I stumbled on a doubt of my design architecture.
I have a page that gets all the Blog entries out of the DB and displays them.
I have a second page that gets all the Comments out of a DB and displays them.

Now after optimizing all of my code I came to the conclusion that bot pages are kind of Identical. So I was wondering what the best way to go is:

Way 1:

index.php
- Lots of Class and variable declaration including my own templating system
- Checking for Nr of posts and making pagination
- Getting all the necessary Blog posts out of DB in while loop.
- During loop setting template variables

comments.php
- Lots of Class and variable declaration including my own templating system
- Checking if user sends info and process it in an object
- Checking for Nr of comments and making pagination
- Getting all the necessary comments out of DB in while loop.
- During loop setting template variables (different ones than blogposts)

Like 70% of the code is the same on both pages.

Way 2:

index.php

- Lots of Class and variable declaration including my own templating system
- if $_GET['action'] == 'comments'
Checking if user sends info and process it in an object
Setting SQL queries for comments
making array with comment template variables
Select comment template
- if $_GET['action'] == entries'
Setting SQL queries for Blog posts
making array with Blog posts template variables
Select blogposts template
- Checking for Nr of to display infos and making pagination
- Getting all the necessary infoss out of DB in while loop.
- During loop setting template variables from the arrays

My Concern is speed vs code redundancy.
If I choose Way 2 the length of the script will increase 60% So in theory it will take longer processing every time the page loads.
In Way 1 only the code needed for particular action is codded in the script so the parser has less work...
On the other Hand, if I find something else to Optimize which is found in both pages I need to change both of them... not really maintenance friendly.

An experienced coder would have a framework -- or at least a library of functions, classes and stuff that deal with most of the issues you've listed. Sorry if this doesn't help you much, but this is part of the definition of an experienced (PHP) coder.

split off redundant code into a separate include file (as you should also do with all of the class definitions etc.) and require() it in both files.

All of my Class codes are in separate php files which I all Include with require_once (I know the difference between include and ...)
I use require_once instead of the __autoload function because opinions are divided on that, some say it is a way to dig yourself in to a big problem.
With class definitions I meant:
$index = new Page();
$Entry = new Data() ect...

The problem with splitting of the redundant code is that it will be messy :/
I tried to look into that and the code doesn't end up clean. To much ugly tricks to be done in my opinion. (Yes I'm a perfectionist )
I'm also concerned if I do find a Clean way, that the class gets too big and that's not optimal if I will use it on other pages where I don't need that redundant code. (I'm even a nitpicker )

Originally Posted by BerislavLopac

An experienced coder would have a framework -- or at least a library of functions, classes and stuff that deal with most of the issues you've listed. Sorry if this doesn't help you much, but this is part of the definition of an experienced (PHP) coder.

I have to disagree with that statement... Frameworks might give you the advantage of speed in development, but it takes away some control and adds overhead... Again this is a choice of the developer and his believes.
I like to have control over every code char I type! I believe that if you want to do code something revolutionary, you do everything yourself!
Plus, Using a framework won't teach me the details of the Internet! I'm just not a fan of pre-packed food that only needs to be heated in the microwave. I like to grow my own crops.

Thanks to both of you, I appreciate your insights, even if I sound stubborn.

Frameworks might give you the advantage of speed in development, but it takes away some control and adds overhead...

Probably, but I wasn't speaking about one of the "named" frameworks out there such as Symphony, Cake or Zend; I meant one of those code collections which all experienced developers have in their toolboxes. That might include all or a part of a more formal framework, but the point is that the said developer knows it intimately and can hack and modify it as a certain project requires.

I like to have control over every code char I type! I believe that if you want to do code something revolutionary, you do everything yourself!
Plus, Using a framework won't teach me the details of the Internet! I'm just not a fan of pre-packed food that only needs to be heated in the microwave. I like to grow my own crops.

Using a framework doesn't really have much to do with the features of your program, so you could just as easily "code something revolutionary" with a framework. Doing everything yourself, without understanding what you're doing, will probably result in you repeating a lot of things other have done. It's an easy trap to fall into, and I probably still haven't gotten out of it, but just be aware.

Also, using a framework will probably teach you more than you think. You don't have to continue using it, but building a site or two will give you some ideas for how everything goes together, and will make your own coding better. To use a better analogy, using a framework is like taking some cooking classes... might not be cooking four course meals any time soon but at least you get the basics down

"Tact is the knack of making a point without making an enemy." - Isaac Newton

I'm not the "getting offend easy" type. So no worries
But I love quotes!

"Imagination is more important than knowledge." --Einstein

Originally Posted by allspiritseve

Using a framework doesn't really have much to do with the features of your program, so you could just as easily "code something revolutionary" with a framework. Doing everything yourself, without understanding what you're doing, will probably result in you repeating a lot of things other have done. It's an easy trap to fall into, and I probably still haven't gotten out of it, but just be aware.

Also, using a framework will probably teach you more than you think. You don't have to continue using it, but building a site or two will give you some ideas for how everything goes together, and will make your own coding better. To use a better analogy, using a framework is like taking some cooking classes... might not be cooking four course meals any time soon but at least you get the basics down

A framework has nothing to with futures, that's right, but it has to do with speed... A framework contains so many options and futures I won't need, I rather create specific things for specific needs.
I can not react to the fact that it will teach me or not because I have never used one before. Maybe one day I will have to; maybe I will never have to... But I will surely take it into consideration. It seems most job offers require knowledge of a framework anyway. We'll see what the future brings.

Originally Posted by BerislavLopac

Probably, but I wasn't speaking about one of the "named" frameworks out there such as Symphony, Cake or Zend; I meant one of those code collections which all experienced developers have in their toolboxes. That might include all or a part of a more formal framework, but the point is that the said developer knows it intimately and can hack and modify it as a certain project requires.

Aha, that way, well then you can say that at this point I am creating my own framework of collection of libs!

But back to the subject...
No real pointers on how I might fix it?
So far I made a temporary fix with a class that extends an other one.
Al tough I don't consider that the most elegant way

thanks for the tip.
The thing is that I already had written my blog, but in procedural php.
And then suddenly I heard everybody talking about this OOP phenomenon.
I decided my code could use a refresh and an optimization. So I recoded everything (I improved lots of small things) and turned everything into class.
The only function I still have is the redirect function (which I can't fit in any relevant class:

The reason why I am so into this is because I want to turn it into my profession. And I heard that you need to really have a good knowledge. No Junior positions anywhere. And since I only have experience in Integrated Circuit design nobody wants me as an experienced programmer yet.
The only thing I can do is nitpick about the smallest things to improve my knowlegde. If you catch my drift.

thanks for the tip.
The thing is that I already had written my blog, but in procedural php.
And then suddenly I heard everybody talking about this OOP phenomenon.
I decided my code could use a refresh and an optimization. So I recoded everything (I improved lots of small things) and turned everything into class.
The only function I still have is the redirect function (which I can't fit in any relevant class:

The reason why I am so into this is because I want to turn it into my profession. And I heard that you need to really have a good knowledge. No Junior positions anywhere. And since I only have experience in Integrated Circuit design nobody wants me as an experienced programmer yet.
The only thing I can do is nitpick about the smallest things to improve my knowlegde. If you catch my drift.

You have more than enough experience to get a position as a programmer. Integrated circuit design is beyond me D:!!!

It's one thing to program with classes, and another thing to program in an object orientated fashion; the two aren't mutually synonymous. I've seen -- hell I've written -- applications that simple use classes as over glorified namespaces -- you can think of a namespace as a container. But that is still fundamentally a procedural paradigm in many ways. Once you start making the switch to program OO you'll start recognizing repetition and commonalities in your code. If you subscribe to the Don't Repeat Yourself DRY mentality which is fundamental to OOP you'll want to find a way to reduce redundancy.

This is about the time I started learning design patterns and that's probably where you will be heading shortly, if you aren't already there. If you aren't, I'd suggest begin checking out design patterns to see how you can implement them. The front controller pattern -- the one I mentioned previously -- is one of the more common patterns that you'll come across. Fluffycat.com, although it's name scares the piss out of me, has a nice catalog of design patterns and implementations in PHP. Martin Fowler's catalog of patterns for enterprise application is another great resource -- I'm assuming you already know UML :3 -- you'll want to bookmark.

Good luck with the transition. But seriously integrated circuits? what the flip? o.O

Creativity knows no other restraint than the
confines of a small mind. - MeGeekly Humor
Oh baby! Check out the design patterns on that framework!