Guidelines for using object or procedural guidance in Python and PHP

Asked

Viewed 96 times

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 hard the study is how PHP implements the manipulation of their 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 POO the information that repeats 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 abstractions of data and procedures that describe the content and behaviour of real-world entities, represented by objects. Otherwise, a class can be defined as a description of the possible properties or states of a set of objects as well as the applicable behaviors or actions to these same objects.

So why while trying to access data on, for example files, arrays and others, or even modify their state I must use functions and not methods that in my view would make sense to be defined in a class and used as method.

I want to point out here that what I am saying may be wrong (after all if I was right I wouldn’t be asking), I would like you to address my doubts as to why the implementation of the two is so different.

  • 1

    Welcome to the world of programming! Inconsistencies like these are common, especially in PHP which is one of the most inconsistent languages!

2 answers

2

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

The code snippet you quoted from PHP is not object oriented. Is a function that takes the value of the variable $frase and makes the replacement.

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

I found this article that has a good explanation of the difference: https://bar8.com.br/abap-oo-versus-procedural-50474ff371a5

2


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

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

There are controversies about these things, and to say that it makes perfect sense requires that you explain why you do it. Using something always requires a justification. Do not use need, just could have one. Never use something without knowing why you are using.

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

And who said that the shape you wrote in Python is really object-oriented? Some people say it is. Some people say it’s just an object-oriented notation, but nothing guarantees it’s OOP. In fact it may just be a language construct.

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

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

I have nothing against certain things being classed. 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 advantage, but in the way people usually use it not always.

Behold: PHP mixes object-oriented codes and procedural language?.

PHP started with one goal, then changed and got a mess, because it’s neither one thing nor completely the other. It’s marketing overriding engineering.

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

So I always say that the foundation is more important.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.