Answer the question
In order to leave comments, you need to log in
Are there any tutorials on OOP?
The question is quite specific.
I am looking for materials in the format code in procedural style -> the same code in OOP style
Yes, someone will say that this does not make sense, but it would still be interesting to compare.
Answer the question
In order to leave comments, you need to log in
Here's a haystack
Here's a bale of hay
What's the difference between the two? And that, and that hay.
But, the bale is compact, it is convenient to fold it, it is easy to carry it, you can even build a house out of it. A haystack is such ... just a heap, it’s not convenient to work with this heap, it crumbles, it blows with the wind, it’s difficult to carry, and you can’t build a house. Those who are familiar with rural life will understand)
So, procedural style is a haystack, OOP is a hay bale. The procedural style is cumbersome, has a huge bunch of some functions with long names, it is difficult to maintain, it is generally difficult to make any changes, and if any are needed, and the site is large, then the same change will have to be changed in the same way in many places, if suddenly you somewhere they forgot to make a change, then at best it will cause an error and you will fix it, at worst everything will continue to work, but it’s not right in that place, try to find this place later ... What OOP gives us ... a lot of things, brevity of the code, its clarity, it is easy to maintain, there is no repetitive code, if you want to change something, it will need to be changed once and nothing will break after that.
As for examples, this example should be large in order to see the advantages of the OOP approach, this is firstly, and secondly, the advantage is especially pronounced when you are working on this project and not when it is already written and working and you look at it from the outside.
Let me remind you again that
what do you want to compare? what looks prettier? By the way, the code written in OOP looks prettier and that's why it's worth using it)
I wanted to remind you that you don't need to compare it, but work with it.
--------------------
Here's an example that won't show all the benefits, it's just an example of one of the many benefits of OOP. The task is to get user data and display it on the screen.
$q = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ? LIMIT 1");
$q->execute([$id_user]);
$user = $q->fetch();
echo 'Имя: ' . $user['name'] . '<br />';
echo 'Фамилия: ' . $user['surname'] . '<br />';
echo 'Город: ' . $user['city'] . '<br />';
class User{
public $id;
public $data;
public function __construct(int $id)
{
$this->id = $id;
$this->data = $this->getData();
}
private function getData(): array
{
$q = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ? LIMIT 1");
$q->execute([$this->id]);
return $q->fetch();
}
public function __get($name)
{
if (isset($this->data[$name])) {
return $this->data[$name];
}
return;
}
}
$user = new User(1);
echo 'Имя: ' . $user->name . '<br />';
echo 'Фамилия: ' . $user->surname . '<br />';
echo 'Город: ' . $user->city . '<br />';
$q = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ? LIMIT 1");
$q->execute([$id_user]);
$user = $q->fetch();
echo 'Имя: ' . ($user['name'] ?? '(данные не заполнены)') . '<br />';
echo 'Фамилия: ' . ($user['surname'] ?? '(данные не заполнены)') . '<br />';
echo 'Город: ' . ($user['city'] ?? '(данные не заполнены)') . '<br />';
class User{
public $id;
public $data;
public function __construct(int $id)
{
$this->id = $id;
$this->data = $this->getData();
}
private function getData(): array
{
$q = $pdo->prepare("SELECT * FROM `users` WHERE `id` = ? LIMIT 1");
$q->execute([$this->id]);
return $q->fetch();
}
public function __get($name)
{
if (isset($this->data[$name])) {
return $this->data[$name];
}
return '(данные не заполнены)';
}
}
$user = new User(1);
echo 'Имя: ' . $user->name . '<br />';
echo 'Фамилия: ' . $user->surname . '<br />';
echo 'Город: ' . $user->city . '<br />';
$q = $pdo->prepare("запрос");
$q->execute([/*переменная*/])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question