F
F
fomenko_alexandr2016-10-28 21:42:12
PHP
fomenko_alexandr, 2016-10-28 21:42:12

How to adopt object-oriented thinking?

Hello masters.
Here I ran into the problem of understanding object thinking. Those. It's hard to understand what to "shove" into one object, what into another, what should be a static method, what should be private, and so on. In general, writing all the time on the procedural, it is difficult to switch to oop.
And now I want to gain experience and knowledge in writing OOP. Tell me which project to start writing (guestbook, blog), or maybe start learning the framework.
Or maybe tell me a book / site where a project is written step by step in OOP, so that understanding comes faster.
Thanks in advance for your replies ;_

Answer the question

In order to leave comments, you need to log in

16 answer(s)
S
Sergey, 2016-10-29
Protko @Fesor

Those. It's hard to understand what to "shove" into one object, what into another, what should be a static method, what should be private, and so on.

Let's try to build analogies. Imagine that your application consists solely of global variables and functions that work with them. I don't think it's hard to imagine. Any variable is available to you at any given time.
That is, in fact, our application is one object. He has everything inside. This object has one method - process the request. When the outside world calls it, the values ​​of some variables change, some internal "private" functions for the outside world are called, and work is done.
Now let's think about the decomposition of all this chaos. We find some task that our code performs (for example, which function to call to process each specific request) and put it into a separate object. Sending emails is a separate object. We sew up all SQL in a separate object. Connection with basis - object. Users are objects. All are objects.
And most importantly, each object has its own area of ​​responsibility. UNIX way. Each object does one thing and does it well. It happens that well ... you need to make one object do two things. NOT a question, we can ask him to do something complicated, and he will, like a good manager, stupidly delegate work to another object. That is, he will do a complicated thing, and he himself will not know how it is done.
And all ownerless functions that do not belong to any objects (for example, functions that generate objects) can be moved to static methods. The main thing is that we do not have static variables (because these are the same global variables). And less public because the devil knows what these developers will use. And "those developers" are you tomorrow.
Just do not think that this is something "fundamentally different". It's the same procedure, just thanks to classes and objects, you can cut the system into small modules. The data will lie next to the procedures and you will have more control over what is happening.
You can start diving into OOP by understanding "why global variables are bad", why "state creates complexity" and what this "complexity" is (many people for some reason think that complexity is expressed in writing code and not in reading it or support) why "isolation" (and by extension encapsulation) is a good thing. How does this all relate to decomposition. What is "responsibility", what are dependencies, connectedness
Frameworks are universal, which means that pure OOP cannot exist there. In any case, there is not a single framework on which it is worth learning OOP.
There are good exercises to develop an understanding of object-oriented design. For example, here: https://habrahabr.ru/post/206802/
I want to note right away that these are extremes. The exercises. They should limit you to make you think and ask the right questions.
So you learn how to do one specific project and on the second you will already lose. That's not how things are done. It is necessary to understand the reasons for the emergence of the idea of ​​OOP. Well, that is what happened before. You can also try to understand functional programming. In PHP, it is poorly applicable, but the main ideas are very closely intertwined with OOP and knowing a little functionality your OOP will be better. Yes, and if you can find a lot of bullshit about OOP, little lies about functionality.

G
GTRxShock, 2016-10-29
@GTRxShock

Browse at your leisure HeadFirst Design Patterns :
www.combook.ru/product/10741424 + as mentioned above, Matt
Zandstra PHP : Objects, Patterns and Programming Techniques :
www.combook.ru/product/11140344 reference point

T
tzlom, 2016-10-29
@tzlom

Very simple.
To begin with, admit to yourself that procedural programming also suffers for you (otherwise you would not have this question), it's not scary, but something needs to be addressed with this too.
Take any of your procedural projects (preferably a small one so as not to get stuck in a routine).
Step one - everything is a function, so you put all the code outside the functions in functions, in total you get that outside the functions there is only a call to main () (or whatever you call it)
The second step - functions work only with what was passed to them. Delete global variables.
The problem of deep nesting appears, i.e. you have B inside call A, and B in it, which wants a variable from A's scope, and there are many such cases. To drag all these variables into B is sad and sad, so we do a trick, each function receives an array of some values ​​​​as the first argument. There is only one rule - functions do not change the names and number of variables in the array, only the values.
The third step - functions should be short, take out repetitive code in separate functions, if a lot of variables are used in a function - this is a reason to break it into several smaller ones.
The fourth step - you are already writing in OOP. If 3 steps are done correctly, then it remains only to arrange this matter according to the new rules - the first array argument is class members, respectively, functions using the same array - methods of this class, you will have to deal with access to fields and individual dependencies, but this will already be simple and understand when you get there.

M
Maxim Timofeev, 2016-10-28
@webinar

Take OOP docks and some framework and spend 48 hours on them. Not 2, but 48. And everything will become clear. Your problem is very common after the 2nd hour of dating.
It will be difficult, you will want to drink, spit and enter the Belarusian random, but you have to endure. It will take 48 hours and everything will fall into place.

O
OnYourLips, 2016-10-28
@OnYourLips

If you plan to stew in your own juice, then be prepared for the fact that a task that another person spends a month on can take you five years.
Don't be lazy to read good code. Try to start with symfony.

A
Artem Spiridonov, 2016-11-03
@customtema

Honestly? Only two books. At least diagonally.
Expansion of consciousness is guaranteed.

R
Rou1997, 2016-10-29
@Rou1997

Tell me which project to start writing (guestbook, blog), or maybe start learning the framework.
And so and so it is possible, and ideally even necessary, in the framework you will learn more about using OOP, and in your project - designing OOP if you think about how to optimize each of the tasks using OOP, and also gain experience from frameworks .
Or maybe tell me a book / site where a project is written step by step in OOP, so that understanding comes faster.
No one will do such an analysis for free, and the majority cannot, therefore, themselves.

E
evgeniy_lm, 2016-10-29
@evgeniy_lm

Psychologists "objective thinking" this is called abstract thinking. Abstract thinking distinguishes Homo sapiens from other living beings. OOP was invented to simplify writing code, the programmer describes the task in images (objects), i.e. as it is. In short, if a programmer is a person, then he doesn’t need to go anywhere, if not a person, then he is unable to write programs

Z
Zhainar, 2016-10-29
@zhainar

Zandstra Objects Patterns and Programming Techniques

A
Alexey, 2016-11-05
@AmberLEX

Dmitry Eliseev
www.elisdn.ru/oop-week
Very chewy and explains clearly.
There are many free videos to understand the level.
This is not another shit-course on how to cut money on retelling the documentation.
Check out the blog posts. I really didn’t take the courses, but I watched the video materials.

K
Kennyx, 2016-11-09
@kennyx

Play JavaRush Explained
quite clearly using the game as an example. At the same time, you can learn Java if you wish.

A
Andrey Andrievsky, 2016-11-03
@andrievski88

Hello, I myself started learning PHP from scratch two months ago, I have already read something and learned about OOP. Look at this course, maybe it will become a little clearer...
https://www.youtube.com/playlist?list=PLSdH7dYnlGY...
And most importantly, do not stop at any difficulties, but take them as a challenge.

D
Denis Shchetinin, 2016-11-04
@chaetal

Try programming in Smalltalk. I do not write "learn" - it is better to learn Smalltalk in practice, preferably on a more or less real project.
Today, perhaps, the most "advanced" implementation is Pharo.
First questions can be asked in the Russian Smalltalk User Group

T
trevoga_su, 2016-10-29
@trevoga_su

book - hail butch
upon the fact of study - look at the real world. everything is an object. one object can be shove into another, connect them somehow, there are interfaces for the interaction of objects
, everything is elementary

T
T86, 2017-03-11
@T86


GRASP Base Craig Larman - Applying UML 2.0 and Design Patterns. 3rd edition
Brett McLaughlin - Object Oriented Analysis and
SOLID Design in PHP https://www.youtube.com/playlist?list=PLoonZ8wII66...
​​Martin Fowler - Refactoring. Improving existing code
R. Martin - Clean code. Building, Analyzing, and Refactoring - 2013
Steve McConnell - Code Perfect 2nd Edition
Advanced
GoF Erich Gamma, Richard Helm - Object Oriented Design Techniques. Design Patterns
Martin Fowler - Patterns of Enterprise Application Architecture
Andrey Bibichev - Domain-Driven Design
Carlos Buenos Vinos - Domain-Driven Design in PHP – 2016
Microsoft Application Architecture Design Guide
Vaughn Vernon - Implementing Domain-Driven Design Methods

A
Anatoly, 2016-11-03
@Skit25

Read books on the topic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question