dongyue3795 2012-07-20 09:21
浏览 41
已采纳

使用PDO和PHP 5构建MVC [关闭]

I am using PDO to make the connection to the DB. I was wondering how should i structure it using the MVC pattern.

Currently i have it like this:

/model/DatabaseConfig.php

/model/DB.php

/model/Model.php

/model/MySQLDB.php

/model/User.php

View/index.php

What do you think about it? Is it ok? Should i place DB, Model and MySQLDB in another subfolder?

Thanks.

  • 写回答

1条回答 默认 最新

  • douguadao3883 2012-07-20 09:34
    关注

    You should have a look at DAO design pattern http://en.wikipedia.org/wiki/Data_access_object. It allows for the separation of Models and Database interface, which will be very useful if at some point you want to switch the database.

    You might also want to read more about it in this article http://www.sitecrafting.com/blog/php-patterns-part-ii/

    It's a matter of preference, but I would structure them in the following manner

    Model/User.php 
    DAO/MySQLDB/User.php 
    DAO/MySQLDB.php (old DB.php)
    Config/DatabaseConfig.php 
    View/Index.php 
    MySQLDB.php
    

    Model would contain all the Model classes with Business Logic in them DAO would contain all the generic database SQL scripts and connections Config would contain all the configuration scripts View all the views Not sure what the DB.php does, so not sure where to place it.

    Steve was asking in the comment about the difference between Model/User.php and DAO/MySQLDB/User.php, hope this example will illustrate it well

    class Model_User
    {
        public function hashPassword($password)
        {
            $salt = SALT_STRING;
            return hash("sha512", $password.$salt);
        }
    
        public function getById($id)
        {
        $id      = (int) $id;
        $db_data = new MySQLDB_User();
        $db_data->getById($id);
        return $this;
        }
    }
    

    and

    class MySQLDB_User
    {
        public function getById($id)
        {
            $sql = "SELECT * FROM `users` "
            ." WHERE `id` = \"" . $this->_db_connection>real_escape_string($id)
            . "\" LIMIT 1";
            $result = $this->query($sql);
            $this->_parseRow($result);
            return $this;
        }
    }
    

    Have a look at how the biggest frameworks structure their folders i.e. Zend Framework example is here http://framework.zend.com/manual/en/project-structure.project.html

    They usually thought long about the set up, and most of the developers are familiar with it, so if you have a new hire, they will be able to find their way around the custom built system quicker than they would otherwise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程