How to correctly describe a PHP class?

Hi all.

How to make a model for working with users?

Option 1:
class Users {
protected $user_id;

public function by_id($id)
{
$this->user_id=$id;
return $this;
}

public function is_exist()
{
/*
$exist = here the piece of code that looks - is there in database user with aydishnik $this->user_id
*/
return $exist ? true : false;
}
}


In this case, I can check if a user already exists:
$user=new Users();
$exist=$user->by_id(1)->is_exist();


Option 2:

class Users {
protected $user_id;

public function by_id($id)
{
$this->user_id=$id;
return $this;
}

public function is_exist($id=false)
{
$id=$id ? $id : $this->user_id;
/*
$exist = here the piece of code that looks - is there in database user with aydishnik $id
*/
return $exist ? true : false;
}
}


In this case, I can check whether the user is already in two ways:
$user=new Users();
$exist=$user->is_exist(1);
$exist2=$user->by_id(1)->is_exist();


The question is whether to make a class in such a way that the same action can be performed in two ways?

Which way would be correct - the one where the method receives an argument directly, or first generating the model in the right form, and then retrieve the correct value.

I also understand that the answer may be "no difference", but how do you exactly?
March 19th 20 at 08:36
3 answers
March 19th 20 at 08:38
Solution
but then I felt really do not understand, what it is I one and the same class will everyone to do. There's already a hodgepodge, and registration, and verification of current Kuksov, and here and ..

This is the difference between the two main architectures ORM - Active Record and Data Mapper.

Data Mapper divides the work with database and business logic of the object itself. In the end we have two objects - User and UserMapper. UserMapper is engaged in communication with the base - searches the database and returns an instance of the User object updates the data in the database, deletes a row from the database. Class User implements only the business logic, knowing nothing about how its data is stored in the database.

Active Record also keeps everything in one class, that is the user is looking for yourself in the database, deletes, and so on.

I think now is the time to move to the option Data Mapper.
Thank you, very clearly and simply. Will.
Shhh
But then :)
- Tyrel_Dougl commented on March 19th 20 at 08:41
March 19th 20 at 08:40
Solution
class User {
 public static findById($id) {
 // Search for our database, and return created object:
 if ($dataFromDB) {
 return new self($dataFromDB);
 // Or something like that:
 $user = new self();
$user->load($dataFromDB);
 return $user;
}
 // If don't find what
 return null;
}
}

But the best is your bike ready to look at ORM'key and choose a suitable time with the object from the database work with the model.
Yes, I agree with you. In the end, will do so:

I will add this last controller in the project tyaplyap, fuck it, not going to redo everything if I have three countersunk two prihlopa and production.

And then immediately start learning laravel and during the acquaintance with its structure at the same time delve into the ORM, and as mentioned above @Rico.Rutherford15 count as adequate people share entities, row data gateway and some kind of repository.

At the same time another tree will be removed, and that still stands. - Tyrel_Dougl commented on March 19th 20 at 08:43
@Tyrel_Dougl, I Advise all to go into "code complete", "Refactoring"(Fowler), PoEAA, "Clean code" and Laravel just to slowly read together )

Laravel will not understand how to do it, just show a couple of ways, and add to the inventory Razrabotka highly questionable practices(hi facades, traits, static) - Rico.Rutherford15 commented on March 19th 20 at 08:46
@Rico.Rutherford15, op, but for literature thanks to separate. - Tyrel_Dougl commented on March 19th 20 at 08:49
March 19th 20 at 08:42
Ever give you any advice until you have read this book and, in particular, Part I. and Part II section called "Chapter 10. Architectural model solutions data sources"

Find more questions by tags PHP