W
W
WildZero2012-01-19 20:41:53
PHP
WildZero, 2012-01-19 20:41:53

PHP, OOP. Practical study

Good afternoon.
There was such a problem. I'm slowly learning OOP. From a theoretical point of view, everything seems to be clear, but due to the lack of practice, I do not "drive" into the very essence of OOP. What practical task can be implemented in order to delve into all the delights of OOP in detail?

Answer the question

In order to leave comments, you need to log in

10 answer(s)
V
Vladimir Chernyshev, 2012-01-20
@VolCh

Any task (solvable in principle) can be implemented both with and without OOP (in a procedural style, for example). The "charms" of OOP are best felt on large projects, where it is impossible to keep in mind all (global) variables and functions and how they interact and come to understand the need to use higher levels of abstraction than individual values ​​and classic arrays of them. You begin to combine semantically related data into structures (associative arrays in PHP), and give functions working with them names prefixed (usually) with the name (purely semantic) of these structures in order to streamline the global namespace a little, getting functions like user_login (array $user, $login, $password), user_logout(array $user) and user_is_logged(array $user).call_user_func($user['user_login'], $user, $login, $password)to put it mildly, it’s inconvenient and redundant, and here in PHP4 they introduce syntax instead and $user->login($login, $password)you can’t live without it anymore :)
But this philosophical digression was about some of the charms of OOP. A direct answer to the question "what task?" - any interesting at least a little bit. Preferably with a developed subject area, in which the presence of several abstractions of different levels is intuitively felt. If for a procedural application you create several tables in the database or use associative arrays to group semantically close values, then this is most likely such a task. For more wow-effect or enlightenment, you can try to write two versions of the same blog type application, one in a procedural style, the other in OOP. Moreover, in the first one it is desirable not to use "intermediate" solutions like associative arrays even for mysql_fetch_assoc/array(), only uncompromising mysql_fetch_row(). If you can’t imagine development without associative arrays,

E
Eternalko, 2012-01-22
@Eternalko

> I don't get into the essence of OOP
The standard task is the user class.
classuser.
Properties
user_id
user_name
user_email
user_pass
Methods.
login
add_new_user
logout
This is where you start to "get it" what OOP is and why it is needed in practice.
Leave encapsulations and polymorphisms for later :)

A
Assorium, 2012-01-20
@Assorium

If you have very basic knowledge, then I can share my own experience. My first two classes were for database and image processing.
The database class provided the connection, the creation of basic query strings (select, insert, update, delete), the provision of multi-table queries, the provision of secure queries (handling tags, extra spaces, removing SQL injections), the database query itself and processing the results. The class also contained stat data, this is the number of hits and the execution time. I still use it, except for very complex queries to the database.
The image class provided (it accepted both one and an array of images): resizing images, moving, cropping, returning statistical data on color, colors, filling, converting an image to b/w, adding a watermark, adding various elements, and even began to write a handler- function approximator. In principle, in terms of complexity and depth in understanding OOP, it does not differ from the first one, but in the future, without changing this class, I wrote a successor class that processed all types of images. I did this only in order to try inheritance with handles, in fact, it helped not to rummage through the old code, but simply knowing the approach, slightly correct the new one.

T
TROODON, 2012-01-19
@TROODON

Write another ORM

I
Ilya Plotnikov, 2012-01-19
@ilyaplot

Until I tried the Yii framework, I did not fully understand OOP and MVC.
On Habré there was an article about MVC OOP. Try to repeat and expand the example.

N
niko83, 2012-01-21
@niko83

OOP is inheritance, encapsulation, polymorphism. Buy a book by Mat Zanstra, it seems the author, everything is neatly arranged on the shelves and read carefully.
In parallel, look at the source code of modern frameworks. And try to write your own simple one, not in order to elevate it to the heights, but to make it at least so that everything works - it develops.

A
Alexey Sundukov, 2012-01-20
@alekciy

What task? Hmm... write a module for drupal, for example. 6th version. And then sit down and think about what problems exist and how they could be solved using OOP. Because, IMHO, in order to understand why OOP was invented at all and why it became so popular in many languages, you need to understand what problems this approach solves. And in order to see the problems to be solved, they must first be found, touched with hands in practice.

K
keyboard_maniac, 2015-01-07
@keyboard_maniac

Grady Booch "Object Oriented Analysis and Design"

E
Egor, 2017-03-10
@egormmm

Write how you wake up in the morning and brush your teeth. OOP is a simulation of the world, only not in real space, but inside a computer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question