G
G
gog692015-07-23 15:54:35
PHP
gog69, 2015-07-23 15:54:35

How to learn to write such OOP code?

I had a test task.
The bottom line: there is a url for which the list of url images, go through them all, resize and display.
I am no longer at the level to write a function that will work from top to bottom. I would create one class and scatter logic according to its methods - i.e. would make methods download, resize, output, etc.
Here's how one guru did it https://github.com/urakozz/tests/blob/master/getIm...
He created 3 classes and used iterators, aggregators, SplFileObject everywhere, in short, classes and patterns for everything that is possible.
How to learn it?

Answer the question

In order to leave comments, you need to log in

12 answer(s)
C
Copperfield, 2015-07-23
@Copperfield

At school, the fizruk told me: "To pull up a lot, you need to pull up a lot."

A
Anton Shamanov, 2015-07-23
@SilenceOfWinter

Matt Zandstra - PHP. Objects, Patterns, and Programming Techniques

D
Dmitry Kovalsky, 2015-07-23
@dmitryKovalskiy

Well, you should start by rereading your own questions. Then go to the book "Head First Design Patterns" just to understand what it is and why it was invented. More carefully (5 times) re-read the chapter where they write that it is not necessary to apply patterns for the sake of applying patterns. And then forward to practice.

D
Dmitry Entelis, 2015-07-23
@DmitriyEntelis

Here's my word of honor, a lot of patterns + oops != good code.
per type code

$this->initBatch();
        $this->attachHandlers();
        $this->executeHandlers();
        $this->getContent();
        $this->detachHandlers();

I would kill slowly.

T
trevoga_su, 2015-07-23
@trevoga_su

there is nothing supernatural in this code,
write more and everything will come with experience

V
Viktor Vsk, 2015-07-23
@viktorvsk

Do not do OOP for the sake of OOP, but solve each problem in an optimal way
Do not consider complex code to be good code: the code by reference will not withstand any criticism at all.
Read https://refactoring.guru/ and all sorts of SOLID terms, but without premature optimization
Not just doing something, but constantly complicating tasks (not to be confused with the complication of the way to solve a problem), wondering if it can be solved easier.

A
Alexander Melekhovets, 2015-07-23
@Blast

If you are interested in code with OOP and patterns for the sake of OOP and patterns - take a look at https://github.com/EnterpriseQualityCoding/FizzBuz... and compare with what happens if you solve the problem head-on. Then ask yourself, what for a goat button accordion? And ask yourself this question all the time to learn how to find a balance between simplicity and flexibility.
Based on the books: Zandstra, then Fowler's PoEAA, then Evans' DDD. We read, apply, think "what for", and so on until enlightenment :)
Good luck)

A
Anton Tikhomirov, 2015-07-30
@Acuna

Colleagues at the top correctly answered that it is not necessary to complicate the code so much, except perhaps for academic interest. The task at your link could fit into one simple function and not make a whole engine out of it. The problem is not OOP, it is very necessary and cool (seriously), but the problem is that it is often put where it is possible, but where it is completely unnecessary. You can hammer nails with a toilet bowl, but why?

P
Pavel Shvedov, 2015-07-23
@mmmaaak

Read and practice what else to advise. There is no magic pill that you eat and learn everything

O
Oleg Krasavin, 2015-07-23
@okwinza

Study the example you provided.
Google places you don't understand (generators, SplFileObject, ArrayIterator, etc.)
Read about built-in interfaces php.net/manual/ru/reserved.interfaces.php
For each of the list, answer the question "Why?" and for what?"
Now try to write something for yourself using these things.
The ability to correctly divide code into classes right off the bat will come with experience.

A
Andrew, 2015-07-23
@Ashlst

Good afternoon.
Learn design patterns, working with SPL, building application architecture.
The most important thing is to write code... a lot of code)

W
Walt Disney, 2015-07-24
@ruFelix

All abstruse approaches to writing code are needed so that it can be written for a long time, refactor faster and maintain for a long, long time, etc.
If you have such tasks, then in practice you will quickly find how to optimize them, using all sorts of abstruse things.
If you use all sorts of abstruse abstractions for the sake of academic interest, and not for the sake of solving production problems, then your code will be complete garbage.
Accordingly, the answer to the question itself: start solving problems where writing such code is vital.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question