O
O
Optimus2017-07-10 22:53:33
PHP
Optimus, 2017-07-10 22:53:33

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

2 answer(s)
A
Alexander Shapoval, 2017-07-11
Pyan @marrk2

Here's a haystack stog.jpg
Here's a bale of hay 617.jpg
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 />';

It would seem that what is the advantage here if there is more code in the OOP from this example, and above I talked about its brevity) brevity will arise over time as our application increases, I also said that you don’t need to admire OOP but work with it, so let’s do it the following, we will make it so that if some data is not filled, then the message "(data is not filled)" is displayed
$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 />';

Have you noticed how the procedural style has changed? We had to change each field, we had to repeat, and it's good that we only have three fields and not tens, and it's good that we have a project from one file and not from .... many. And it's also very good that PHP7 came out, otherwise I had to fence a bunch of conditions.
Have you noticed how the OOP code has changed?
In one place, a piece of code has changed, it was:
Became: Such cases will appear all the time, so the choice of an OOP approach is obvious.
The advantage of OOP will be even more clearly shown if we add editing the questionnaire, in a procedural style it will be necessary to create three queries (because in our example there are three fields, in a real project there may be dozens of them), in OOP you will have to tinker a little once (just a little ) and make a universal data update, which as a result, exactly in the place where the update is required, it will turn out like this:
Procedural style:
$q = $pdo->prepare("запрос");
$q->execute([/*переменная*/])

And so three times (in our example, but in a real project more)
you just need to write like this (for example, change the name) We will not just change the property, the data will change in the database, just one such line, but how is it let it be homework, because it's already three in the morning and I'm going to bed, good to everyone)

D
Dimonchik, 2017-07-10
@dimonchik2013

php design patterns
on Google
https://github.com/domnikl/DesignPatternsPHP

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question