How to implement database access in the user class?

Want to create a user class, but as it to access database?
Is it okay to do this?
class User{
private static $db = ";

 public static function setup($type, $user, $pass, $opt)
{
 self::$db = new PDO($type, $user, $pass, $opt);
}
}
User::setup($type, $user, $pass, $opt);
April 19th 20 at 12:32
3 answers
April 19th 20 at 12:34
Is it okay to do this?

Of course fine.
Will, for example, the application 10 different classes and have 10 database connections. 100 class - 100 connections. What's wrong with that?

How to implement database access in the user class?

In the User class there should be no access to the database. But to understand this you five years.

So you can just connect to the database and pass in the class already finished connection
public static function setup($pdo)
{
 self::$db = $pdo;
 }


But given that you have no OOP and shit prozeduren, you can just write global $pdo in the "methods" of his "class" and not to be soared
well, as I then should do? if you make a separate class with the database connection? - Jaquelin52 commented on April 19th 20 at 12:37
not to do so. global write and do not worry.
I can't recount here several thick books in which is written how it is necessary to do and why - berry11 commented on April 19th 20 at 12:40
@berry11, and if you make a connection class which will access the database and to register
class User extends PDOconn{} - Jaquelin52 commented on April 19th 20 at 12:43
Why? - berry11 commented on April 19th 20 at 12:46
@berry11, well, how else to access the dB if it is for example in
class Connection{
private static $db = ";
}
- Jaquelin52 commented on April 19th 20 at 12:49
Why do you have a Connection class? - berry11 commented on April 19th 20 at 12:52
@berry11,
In the User class there should be no access to the database. But to understand this you five years. - Jaquelin52 commented on April 19th 20 at 12:55
first, the 5 years was up.
second, you still try to push the work with the DB class user.
this class should not be database queries. but you do not understand, because for you these queries is the whole point of the class user.
so don't try to do anything you don't understand - berry11 commented on April 19th 20 at 12:58
@berry11, I Have a big class which implements CRUD, need to it turns to go? - Jaquelin52 commented on April 19th 20 at 13:01
I don't know.
But if there is, then try to ask - berry11 commented on April 19th 20 at 13:04
@berry11, I mean it will be right? can you see the code? I'll post on github - Jaquelin52 commented on April 19th 20 at 13:07
can - berry11 commented on April 19th 20 at 13:10
@berry11, Code - Jaquelin52 commented on April 19th 20 at 13:13
well, you can use. although I do not understand why you need it - berry11 commented on April 19th 20 at 13:16
@berry11, well, how else will right? so five years flew by in 1 hour - Jaquelin52 commented on April 19th 20 at 13:19
:) - berry11 commented on April 19th 20 at 13:22
@berry11, as I understand it is necessary to create a class that will just give the connection to the database? - Jaquelin52 commented on April 19th 20 at 13:25
this class in PHP already. called PDO - berry11 commented on April 19th 20 at 13:28
April 19th 20 at 12:36
Not ideal, but you can try something like this
and then if I want to make a function to get user
public static function findById(){
connection::getInstance()->
}

? - Jaquelin52 commented on April 19th 20 at 12:39
Vital. Are you serious? that's right really? - berry11 commented on April 19th 20 at 12:42
April 19th 20 at 12:38
There's the next thread @berry11 correctly wrote that to understand why it is not necessary to do - you need experience.

But if you try very briefly and without introduction of the "extra classes", all the same, I think, is the whole thing a little can be improved:
1) For storage, users must meet storage users
2) user should meet the user
3) For the connection to the database connection to the database

Your captain obvious)

There seems to be clear? Then do:
// Connecting to DB
$connection = new PDO(...);

// Store with a "findById"
class UsersDatabaseRepository
{
 public function __construct(PDO $connection) { ... }

 public function findById(int $id): ?User
{
 $stmt = $this->prepare('SELECT * FROM `users` WHERE id = ? LIMIT 1');
$stmt->execute([$id]);

 return new User($stmt->fetch(PDO::FETCH_ASSOC)) ?: null;
}

 public function save(User $user): void
{
 // ...
}
}

// The user with one field "id"
class User
{
 public int $id;
 public function __construct(array $attributes) 
{
 $this->id = $attributes['id'] ?? 0;
}
}

// As a bonus: we Now have one and the same user can be stored anywhere! 
// User and itself we did not touch - just added a new class that stores all the files
class UsersFilesystemRepository 
{
 public function findById(int $id): ?User { /* some code */ }
}

// As a bonus N2: And we can do the same to this 
// user via the API from GitHub
class UsersGithubRepository { ... }


In the world everything is a little more complicated. For example:
1) to Pass an array to the constructor of the User - it's not pretty.
2) findById overly complicated, and it is possible to simplify, to endure such a "similar" code on a higher level in AbstractDatabaseRespotory
3) Work with turnip (storage) is through the interface (or rather through their set), and not through implementation.
And so on...

In any case, at least, this separation will allow you easier to organize the code.

Find more questions by tags PHP