Naming a class which fetches data from database


Right now I’ve got a file called invoices.php which contains a lot of functions which works with invoices in the database. It has functions like getInvoice($id), getInvoiceItems($invoiceId), etc.

I would like to make that file into a class so it doesn’t pollute the global namespace. However I’m not sure what to call it. I could just call it InvoiceManager but I know it’s said that you should really think about what to call a class before naming it *Manager, *Handler, etc. I’m not sure if classes like this has a design pattern name?

What would you name it?

Also I don’t want to create more advanced design patterns to work with my data I just want to decide what to name this one :slight_smile:


It sounds like a (Data)Mapper to me. :slight_smile:

I am going to assume your referring to a collection of related functions not written using a ORM. In that case something like DAOInvoice (data access object) is probably more appropriate than naming the class a mapper, which it doesn’t seem like it would be. I mean if you were using an ORM everything would already b object-oriented essentially. You could also consider using a namespace instead of a class if using php 5.3.

Thanks both of you! I’ve read about Data Mapper and Data Access Objects and what I have is a DAO so I went with InvoiceDAO.

I don’t want to use namespaces because I don’t really need them and they make the code look even more complex (and really ugly).

you should try Data Mapper hope you will get solution

I would call it a DAL if it contains all DB access functions for the application or DAO if there are more functions and these are just the ones for the Invoices. Either way “DalInvoices” or “DaoInvoices”. That’s what I do in my applications.

It’s not a DAL - a DAL would be something like the Pear_DB package as it abstracts the data access to support e.g. different drivers like PDO, mysql, mysqli etc.
If the class directly executes SQL and returns the rows it also is not a mapper as it does not map the data to ValueObjects or DataTransferObjects.
So what’s left is a DAO as it provides methods to access data.

What I’ve described above is not set in stone, it’s only the way I’d categorize my classes.
If I had to implement it I’d go with a Mapper that asks the DAO to fetch it’s data from the DAL and map it to DTOs, which all together end up in a DomainObject :slight_smile: