SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    How would the experienced coder look at this?

    Thanks for any tips.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  2. #2
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    How would the experienced coder look at this?
    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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    way 3:

    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.

  4. #4
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by philjohn View Post
    way 3:

    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 )



    Quote Originally Posted by BerislavLopac View Post
    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.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  5. #5
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    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.

  6. #6
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    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

  7. #7
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!
    It is OK, you will grow out of it

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "If I have seen further it is only by standing on the shoulders of Giants." - Issac Newton
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    "Tact is the knack of making a point without making an enemy." - Isaac Newton

  10. #10
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    "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


    Quote Originally Posted by allspiritseve View Post
    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.


    Quote Originally Posted by BerislavLopac View Post
    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
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  11. #11
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "premature optimization is the root of all evil."

    Build it first. Build it well. Worry about optimization later. Optimization kills creativity and complicates the hell out of everything.

    As far as references -- cause PHP can't do pointers? hehe... sorry ._. -- Front Controller Pattern might be of interest to you.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  12. #12
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    "premature optimization is the root of all evil."

    Build it first. Build it well. Worry about optimization later. Optimization kills creativity and complicates the hell out of everything.

    As far as references -- cause PHP can't do pointers? hehe... sorry ._. -- Front Controller Pattern might be of interest to you.
    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:

    PHP Code:
    function redirect($Location)
    {
        
    header('Location: ' $Location);
        exit;

    Everything else turned into methods and properties.

    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.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  13. #13
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    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:

    PHP Code:
    function redirect($Location)
    {
        
    header('Location: ' $Location);
        exit;

    Everything else turned into methods and properties.

    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.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •