dongye9182 2013-03-24 22:17
浏览 32

将函数与辅助类分开是否合适?

I'm pretty new to the MVC concept itself but I'm slowly starting to grasp it, and what it is that makes it so useful.

I have in my app different tabs which are levels in a hierarchy (I call them entities), each represented by two classes - a Binding class with all the methods to communicate with the database and a helper class which is just a skeleton of the Entity, both in same file. I wonder if this is the right way to approach the design pattern? I haven't built a Model or a Controller yet because I'm not sure where they'll come in at the moment. Also I'm not interested to use ORM right now.

class CompanyBinding extends EntityBinding {
    function __construct() {
        parent::__construct();
    }


    public function get($criteria = array()) {
        // method to retrieve an array of DB rows, each represented by the helper class

            [...]

        return $sth->fetchAll(PDO::FETCH_CLASS, 'Company');
    }

    public function get_by_id($id) {
        // method to retrieve an one single object of the helper class

        $sth->setFetchMode(PDO::FETCH_CLASS, 'Company');
        return $sth->fetch();
    }
}


class Company extends Entity {
    public $id;
    public $name;
    public $email;
    public $phone;

    // just properties. some classes have a __construct() function to manipulate some fields after retrieval

    [...]
}

Do I need a model class now? I was considering the Controller could perform the ->get function and load appropriate class, because the name of the entity is passed from the front-end with AJAX.

By the way I also am really confused. Should the controller absorb all AJAX requests or should I make a file called ajax.php that handles that, and put the Controller class above public_html so users can't access the class file?

  • 写回答

1条回答 默认 最新

  • dongshui9690 2013-03-24 22:55
    关注

    Separate the classes into their own files. PHP does a decent job at caching file locations once it opens them, so you don't have to worry about IO. And generally speaking, it is bad practice to put multiple classes in one file. It won't break your application, but it may cause more seasoned developers to break-down into tears. :)

    Apart from that, you are fine.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助