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 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作