Dependency-injection in real life

后端 未结 3 1275
温柔的废话
温柔的废话 2021-01-31 19:10

I am building a really minimal MVC framework to increase my PHP knowledge and challenge myself. I\'ve come to the point where Classes begin to be dependent on each other to work

3条回答
  •  隐瞒了意图╮
    2021-01-31 19:49

    If you are worried about multiple simultaneous connections you can just use mysql_pconnect() or the equivelant for the database you are using. It will check if a connection is already open and use the existing connection if it is.

    As far as the container issue, I've seen it done in two ways, which you seem to be aware of both. The first method is to have the framework read your database schema and create classes fore each table. I personally don't like this approach. Symfony is one framework that does this (by using the doctrine ORM).

    The more preferred method I've seen is to have a generic container, which basically builds the sql for you given a table, columns and an action. This is the approach taken by codeIgniter:

    $query = $this->db->get('mytable');
    $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
    

提交回复
热议问题