SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Let me know if my MVC implementation is okay

    Hello I need help from someone who is very good in MVC. Please tell me if my MVC implentation is okay or if it can be improved or if it is totally wrong. Thanks a lot! Here it is:

    The class file:
    PHP Code:
    <?
     
    // msyql.php
    class mysql
    {
     
    var 
    $link;
    var 
    $result;
     
    function 
    connect ()
    {
    $this->link mysql_connect();
    $this->link mysql_select_db();
    }
     
    function 
    disconnect ()

    mysql_close($this->link);
    $this->link false
    }
     
    function 
    query ($sql)
    {
    return 
    $this->result mysql_query($sql); 
    }
     
    }
     
    // model.php
    class model
    {
     
    // insert application wide stuff here
     
    }
     
    // controller.php
    class controller
    {
     
    var 
    $dao
     
    function 
    controller ($dao$controller)

    require_once 
    $controller.'.php';
    new 
    $controller($dao);
    }
     
    }
     
    // productModel.php
    class productModel extends model
    {
     
    function 
    productModel($dao)
    {
    $this->dao $dao;
    }
     
    function 
    getProducts ()
    {
    return 
    $this->dao->query('SELECT * FROM products');
    }
     
    }
     
    // productController.php
    class productController extends controller
    {
     
    function 
    productController ($dao)
    {
    $obj =& productModel($dao);
    $arr_products =& $obj->getProducts();
    require_once 
    'productView.php';
    }

     
    ?>
    The View file:
    PHP Code:
    // productView.php
    while ($row mysql_fetch_array($arr_products))
    echo 
    $row['prod_id'].' - '.$row['prod_name']; 
    Usage:
    PHP Code:
    // index.php
    $dao =& new mysql();
    new 
    controller($dao$_GET['controller']); 

  2. #2
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ohhhh no replies? Is it a little hard to understand? I'll try to explain. Basically, classes' model, controller are suppose to be application wide classes. mysql class is just a db abstraction class. productModel, productController, and productView are page specific. So if I want to use the products page. I just call the index page like this: index.php?controller=product

    Can someone please help, Ezku? Dr Livingston? I just need an advice. Thanks.

  3. #3
    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)
    new controller($dao, $_GET['controller']);

    Then

    function controller ($dao, $controller)
    {
    require_once $controller.'.php';

    makes me a little leary about using input directly from the user without sanitizing it. It is also trivial with this setup to cause a PHP fatal error (just change the controller=whatever to something that does not exist).

    Also,

    function productModel($dao)
    {
    $this->dao = $dao;

    is not catching and storing the $dao by reference. This would make it impossible to test with a mock object as you would lose the reference to the object.

    Otherwise it looks very similar to the example of MVC Harry has up on the phppatterns.com site.

    HTH
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Otherwise it looks very similar to the example of MVC Harry has up on the phppatterns.com site.
    which is not a very good example (Controller extends View??)
    I once had a problem.
    I thought: "Oh, I know: I'll just use XML!"
    Now I had two problems.

  5. #5
    SitePoint Enthusiast alpeb's Avatar
    Join Date
    Jul 2004
    Location
    Colombia
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys, can you access phppatterns.com? It's been several days I've been trying without response

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the first time in several years, it appears that the web site is down at the moment?

    Proberly due to no new content since Harry has went underground, so to speak

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looking at php patterns just a few minutes ago, it appears that the site is back online again... And wait for it... Harry Feucks has just gone and posted a new Article on using Python to switch between different versions on PHP on the same box.

    Apparantly?

  8. #8
    SitePoint Enthusiast alpeb's Avatar
    Join Date
    Jul 2004
    Location
    Colombia
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn, is down again! Next time I catch it online I'll print it!

    I would be delighted to provide free quality hosting for that site (just in the remote case you read this Harry...)


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
  •