Best practices for setting up mysql PHP connection

I was wondering if there is a general practice for setting up the mysql connection object in a php program so that you don't repeat the excess connection code in your classes.

+3
source share
2 answers

I use the repository template ( http://martinfowler.com/eaaCatalog/repository.html ) to put my entire database and SQL code in a small set of classes that are responsible for translating between the database and domain objects. This allows the rest of the code to have redundant db connection code or SQL queries.

I use it as follows:

$criteria = new UserCriteria();
$criteria->active = true;
$repository->getUsers($criteria); // repo connects to DB and returns array of user objects
+2
source

, , , .

, , , .

0

Source: https://habr.com/ru/post/1739471/


All Articles