Advanced Search

Results 1 to 8 of 8

Thread: php structure (MVC?)

  1. #1
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default php structure (MVC?)

    Hello everyone

    Im about to start a new project at home and this time I want to create everything in an organized way and hopefully I learn how to work with PHP OO with css based xhtml code.

    Anyways, I know that ASP now is MVC based (Model-view-controller) so that you easily use and reuse componenets and I also heard that PHP doesn't have that good support for this. But I don't know anything about ASP and would rather use PHP so is there some golden rule how you should work in php?
    I know that static functions is a good option when you work with componenets, for example:
    PHP Code:
    myClass::Button("this is my button label"); 
    Is this the best way to create and recreate componenets in PHP 5?

    And should I still use the old:
    PHP Code:
    include('right_side.php'); 
    or should I use static functions here too?

    Last question:
    I want to write everything inside php tags (<?php and ?>)
    so how can I keep the xhtml code with a good structure, for example:
    PHP Code:
    echo "<div>...</div>";
    echo 
    "<div>...</div>"
    would output <div>...</div><div>...</div> in one single line, will I have to add \n in the end of every linebreak I want to make?

    I probably have some more questions when I get going but for now this will do.
    Last edited by Dennis_Gull; 09-03-2008 at 01:23 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    In reference to your second question, yes - you would have to use \n in order to create a line break. My advice is to not use the php tags for XHTML output. For example:

    PHP Code:
    <?php 

    // php scripting here

    ?>

    <div>...</div>
    <div>...</div>

    <?php 

    //more php here

    ?>
    It's cleaner and faster to parse
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    MVC and components are mostly contradictory philosophies when it comes to web design, with MVC being the most popular at the moment. It is not true that PHP doesn't have good support for MVC. PHP's OO is somewhat lacking in a few areas, but MVC does not require OO at all (my favourite implementation in PHP does not use OO), and it appears to be quite possible to hack around these limitations sufficiently in most cases.

    include() (or, usually preferably, require_once()) are of course still useful, but you should not be using files with names like 'right_side.php' because this indicates that the code is strongly tied to the output, something that you want to avoid at all costs (and said avoidance is one of the primary motivations for the MVC pattern in the first place).

    Likewise, you should not be handling strings of output in your PHP code. Your output must be separated from the rest of your code as much as possible — this is the V part of 'MVC'. You will want to have your 'templates' in completely separate files, probably in a separate directory, and certainly not mixed in with your controllers. A templating engine can be used to greatly ease this separation. If you're working with databases, an ORM will also help cut down on the controller code by quite a bit.

    At this point in time, it's almost certain that you do not wish to be working with XHTML, as IE has no support for it. http://www.webdevout.net/articles/beware-of-xhtml has some further information on this, if you're interested. You should almost certainly be sticking to HTML 4.01 Strict.

    There are a variety of MVC PHP frameworks available pre-written for you (http://codeigniter.com/, http://cakephp.org/). While it isn't particularly difficult to write your own once you have a good feel for the MVC pattern, as a beginner it will probably help you learn to use one as a guide rail.

    Most of this information and some other associated common pitfalls are described in my post at http://dynamicdrive.com/forums/showpost.php?p=158839.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the great inputs, I will start by taking a look at some of the pre-written frameworks for MVC

  5. #5
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    include() (or, usually preferably, require_once()) are of course still useful, but you should not be using files with names like 'right_side.php' because this indicates that the code is strongly tied to the output, something that you want to avoid at all costs ...
    Just out of curiosity what should I name the side panels?

    Quote Originally Posted by Twey View Post
    Likewise, you should not be handling strings of output in your PHP code. Your output must be separated from the rest of your code as much as possible
    What if I have an array with a couple of values I want to print?
    Lets say I select the 10 latest posts and want to output them dynamically, wont I have to do it all in php then?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Name them whatever you like — but they should be templates, not PHP files, and you'll render() them, not include() them.

    -–—
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb

    Quote Originally Posted by Twey View Post
    There are a variety of MVC PHP frameworks available pre-written for you (http://codeigniter.com/, http://cakephp.org/). While it isn't particularly difficult to write your own once you have a good feel for the MVC pattern, as a beginner it will probably help you learn to use one as a guide rail.
    Fuse is also one of the big contenders in the PHP MVC arena: Fuse PHP MVC Framework


  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well, you certainly seem to be promoting it enthusiastically, and I hate to disillusion you here, but that doesn't automatically make it a 'big player' *amusement*

    I'll leave it, though, since as a new framework some people might find it easier to read as an example of the basic concepts. Beware, though, that it's not nearly as featureful as the others, and you probably don't want to be using it in production.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •