Answer the question
In order to leave comments, you need to log in
How to correctly form a project structure in PHP?
The question is somewhat general.
I've been developing in PHP for a long time. Basically - structured programming, OOP had to be used a little. I kind of understand the principles. But sometimes I get confused logically.
Essence:
I want to realize my small project. Of the closest analogues - twitter, but not in meaning, but in structure. There will be a class of users, an abstract class of added "publications" (of various kinds).
The main question is how to correctly organize work with the database? It would be logical, it seems to me, to write your own class for this, but I don’t really understand how to work with it in this case? For example, my user has a getPublication method that should query the database and get all of his publications, how do I access the database? Create a method "q" (for example), in which to pass a query to the database as a parameter? And this method should address each time at first to a method of a connection to a DB? And at the end close the connection by calling the close method?
Before that, I had experience only with the Flight micro-framework, which does not have a class for working with the database, and with a self-written (foreign) framework in which the sql object was global. I don't like this way.
I don’t use frameworks, because I want to write myself in order to figure it out.
I understand that the questions are not clear and somewhat stupid, but I would like, as I said, to understand normally.
Thanks
Answer the question
In order to leave comments, you need to log in
If you want to write your own ORM, then take a couple of ready-made ones from some frameworks, see how they work, take the best of them and write your own). I also recommend reading this book by Fowler www.williamspublishing.com/Books/5-8459-0579-6.html , chapters 3, 9, 10 - there is exactly what you are asking about.
A similar question about the organization of interaction with the database is often asked.
In short, there are two popular templates DataMapper and ActiveRecord. The first one is considered more correct in terms of OOP canons.
Like many, I also wrote my own microframework at one time (and I use it from time to time).
You can look at my simple implementation of DataMapper (DAO): base class and child
https://github.com/tonydspaniard/yiinitializr-basic download, install, use. framework in php.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question