Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I guess there are actually two items in the question:</p> <ol> <li><p>As @duskwuff points out, there is nothing wrong with static methods per-se; if you know their caveats (e.g. "<a href="http://php.net/manual/en/language.oop5.late-static-bindings.php" rel="nofollow">Late Static Binding</a>") and limitations, they are just another tool to work with. However, the way you model the interaction with the DB does have an impact on separation of concerns and, for example, unit testing. </p></li> <li><p>For different reasons there is no "absolute proper way" of doing persistence. One of the reasons is that each way of tackling it has different tradeoffs; which one is better for you project is hard to tell. The other important reason is that languages evolve, so a new language feature can improve the way frameworks handle things. So, instead of looking for the perfect way of doing it you may want to consider different ways of approaching OO persistence assuming that you want so use a relational database:</p> <ul> <li>Use the <a href="http://en.wikipedia.org/wiki/Active_record_pattern" rel="nofollow">Active Record</a> pattern. What you have done so far looks like is in the Active Record style, so you may find it natural. The active record has the advantage of being simple to grasp, but tends to be tightly coupled with the DB (of course this depends on the implementation). This is bad from the separation of concerns view and may complicate testing.</li> <li>Use an ORM (like <a href="http://www.doctrine-project.org/" rel="nofollow">Doctrine</a> or <a href="http://www.propelorm.org/" rel="nofollow">Propel</a>). In this case most of the hard work is done by the framework (BD mapping, foreign keys, cascade deletes, some standard queries, etc.), but you must adapt to the framework rules (e.g. I recall having a lot of problems with the way Doctrine 1 handled hierarchies in a project. AFAIK this things are solved in Doctrine 2).</li> <li>Roll your own framework to suite your project needs and your programming style. This is clearly a time consuming task, but you learn a lot.</li> </ul></li> </ol> <p>As a general rule of thumb I try to keep my domain models as independent as possible from things like DB, mainly because of unit tests. Unit tests should be running all the time while you are programming and thus they should run fast (I always keep a terminal open while I program and I'm constantly switching to it to run the whole suite after applying changes). If you have to interact with the DB then your tests will become slow (any mid-sized system will have 100 or 200 test methods, so the methods should run in the order of milliseconds to be useful). </p> <p>Finally, there are different techniques to cope with objects that communicate with DBs (e.g. mock objects), but a good advise is to always have a layer between your domain model and the DB. This will make your model more flexible to changes and easier to test.</p> <p>EDIT: I forgot to mention the <a href="http://en.wikipedia.org/wiki/Data_access_object" rel="nofollow">DAO</a> approach, also stated in @MikeSW answer.</p> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload