Guidelines for using object orientation or procedural in Python and PHP

1

I am studying PHP and Python and I am feeling a huge difficulty, not in relation to concepts, after all they are independent of language, what is making the study hard is how PHP implements the manipulation of its objects, follows an example :

// Em PHP
>>> $frase = "Lorem ipsum";
>>> echo str_replace(" ", "<br/>", $frase); // "lorem<br/>ipsum"  

# Em Python

>>> frase = "Lorem ipsum ...";
>>> print(frase.replace(" ", <br/>))

Whenever you start your studies in OOP, the information that repeats the most is:

  

In object orientation, a class is a description that abstracts a   set of objects with similar characteristics. More formally, it is   a concept that encapsulates data abstractions and procedures that   describe the content and behavior of real-world entities,   represented by objects. Otherwise, a class can be   defined as a description of the properties or possible states of   a set of objects, as well as applicable behaviors or actions   to these same objects.

Then because when trying to access data in, for example, arrays and others, or even modify its state, I must use functions and not methods which in my view would make sense to be defined in a class and used as method.

I want to emphasize here that what I am saying may be wrong (after all, if I were right I would not be asking), I would like you to remedy my doubts as to why the implementation of the two is so different.     

asked by anonymous 27.08.2018 / 20:45

2 answers

2

What does not count in these courses and tutorials is that OOP is completely unnecessary in any situation. It is useful in some cases because it organizes better and attends certain lines of thought. Rare problems where object orientation is key. In GUI for example it is enough. In business domains it is seldom necessary to actually do so. In script languages, especially those that execute and end OOP parts is a complete exaggeration . But they managed to misrepresent this.

There are several definitions of what OOP is, , which already shows how complicated it is to work. Some say OO is even a paradigm.

There are controversies about these things, and saying that makes perfect sense requires you to explain why you do so. To use something always requires justification. Not to use does not need, can only have one. Never use something without knowing why you are using it.

What gain do you expect to have in the second code compared to doing the first one? I see one, and only one (or two), but I will not say: P

And who said that the form you wrote in Python is actually object-oriented. Some people say it is. There are people who say it's just an object-oriented notation, but there's no guarantee it's OOP. In fact it may be just a language construct.

I even like object-oriented notation, the paradigm a little less.

And if so, what advantage did it have? Did he really need this? And if you do wrong, is it as useful as it sounds? Does not it have a middle ground?

I have nothing against certain things being put into classes. By some definitions (mine) this is not object oriented, it only involves encapsulation, or even that, it only involves a certain abstraction. For types it may be a good one, for mechanisms it has advantages, but in the way people usually do not use.

See: PHP blend object-oriented code and procedural language .

PHP started with a goal, then changed and became a mess, because neither is one thing nor is completely the other. It's marketing overlapping with engineering.

What worries me the most is that most people "use" OOP without understanding it and I did it for decades, until I understood OOP better and started using it better, and less because I used it where it was appropriate. And I stopped calling OOP what it was. Stack Overflow (EN and PT) was important for me to become more questioning and have a place to see the experience of other people. To put my knowledge to the test with people who know certain things better than I do.

So I always say the wrapping is more important.

    
27.08.2018 / 20:56
2

Understanding your question is as follows: You can develop a PHP application with procedural and / or object-oriented code.

The code snippet you cited from PHP is not object-oriented . It is a function that takes the value of the variable $frase and does the substitution.

In Python, on the contrary, you are calling a method of class str to perform the same procedure, OOP.

I found this article that has a good explanation of the difference: link

    
27.08.2018 / 20:56