Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

apps/myapp/models/ vs apps/myapp/lib/

edited September 2013 in Apps

I'm not quite clear on what type of functions or methods I'd keep in apps/myapp/models/Myapp.php vs the ones I'd keep in apps/myapp/lib/Mylibrary.php

Is the models folder for things that relate directly to the model ("Model", typically), that my model is extending, while the lib folder is more of a general-purpose library?

And should apps/myapp/models/Myapp.php start with

namespace myapp;

class Myapp {

while apps/myapp/lib/Mylibrary.php would start with

class Mylibrary {

?

And if I define several apps for a site, does it really matter which lib file I put things in? I can call them from anywhere, right?

Comments

  • The best practice is to use the app name as a namespace for all your models and libs, so each would start with:

    <?php
    
    namespace myapp;
    
    class ClassName {
    

    Generally, if it's fetching or storing data, or adding a bit of logic around the data fetching/validation/storage, then it should be in your models. If it's non-related, like helper methods, then that would go in lib.

    Take a look at apps/blog/models/Post.php for some examples of custom methods added to a model, versus something like apps/blog/lib/CsvParser.php which doesn't deal with the data model so I put it in lib.

  • Thanks. And the file name should match the class name in either case (lib or models), right?

  • That's right, that way the autoloader can find them without using require() calls.

Sign In or Register to comment.