It is not feasible to try to correct the code you posted because OOP is conceptual and depends on how is the structure of the other codes of the system you are developing.
In the code you posted there is the classic getter of getter and setter
(popular term). The technical name is "accessors and mutators".
However, it is applied in a redundant way, as the property nome_do_user
is defined as public. That is, it can be accessed freely without accessors or can be modified freely without mutators.
$correios = new Correios();
var_dump($correios->nome_do_user);
To correct and arrive at a more suitable path in object-oriented programming, try to understand the basic definition of visibility of properties and methods: http://php.net/manual/en/language.oop5.visibility.php
Example with accessors and mutators (getter and Setter)
Returning to code, even if it summons $correios->nome_do_user
, you still won’t get anything from nome_do_user
because it was not set.
To set, invoke the method RecebeJson()
before accessing nome_do_user
:
$correios = new Correios();
$correios->RecebeJson();
var_dump($correios->nome_do_user);
Okay, it worked! But it didn’t solve anything in OOP properly speaking.
class Correios
{
/*
Definimos com visibilidade privada, para que seja acessível somente dentro desse objeto Correios.
*/
private $nome_do_user = null;
public function setNomeUser()
{
/*
Uma condicional que verifica se está vazio. Isso evita executar a rotina mais de uma vez.
A lógica aqui é que se já foi executado, provavelmente não precisa executar mais vezes pois apenas estaria consumindo processos.
*/
if ($this->nome_do_user === null) {
$this->nome_do_user = json_decode(file_get_contents('php://input'), true);
}
}
/*
Accessor que retorna a propriedade nome_do_user.
*/
public function getNomeUser()
{
return $this->nome_do_user;
}
}
$correios = new Correios();
$correios->setNomeUser();
var_dump($correios->getNomeUser());
As you can see, in practice, much has been modified from the original.
Example deleting the mutator (Setter)
Speaking personally, despite the attempt to demonstrate a way to correct, I feel uncomfortable because it does not mean that this is correct or the best to do, because as I said at the beginning, OOP is conceptual. It’s a set of paradigms. But let’s not get into this discussion because it’s something very extensive. Let’s go to a new suggestion to improve, or optimize this class:
What bothers me about this class is that the use of a mutator. In that case setNomeUser()
. We can simplify it like this:
class Correios
{
/*
Definimos com visibilidade privada, para que seja acessível somente dentro desse objeto Correios.
*/
private $nome_do_user = null;
public function getNomeUser()
{
/*
Uma condicional que verifica se está vazio. Isso evita executar a rotina mais de uma vez.
A lógica aqui é que se já foi executado, provavelmente não precisa executar mais vezes pois apenas estaria consumindo processos.
*/
if ($this->nome_do_user === null) {
$this->nome_do_user = json_decode(file_get_contents('php://input'), true);
}
return $this->nome_do_user;
}
}
$correios = new Correios();
var_dump($correios->getNomeUser());
Still this may not be ideal for your project, because it depends on planning, what else you intend to implement in this class.
Despite this, note that we are now leaving the "getter and Setter" pattern (accessor and mutator) and creating a new standard.
The points you should note is, the conceptual standard you are creating is something existing, widely recognized and accepted by developer communities?
Even if it’s a different pattern from existing and widely recognized standards, I can create a pattern of my own?
As long as it’s well-documented, yes. And obviously as long as it’s consistent, and well-written.
Example with constructors
Let’s go to a third example of how to "refine" or "complicate" what is already complicated.
class Correios
{
/*
Definimos com visibilidade privada, para que seja acessível somente dentro desse objeto Correios.
*/
private $nome_do_user = null;
public function __construct()
{
/*
Uma condicional que verifica se está vazio. Isso evita executar a rotina mais de uma vez.
A lógica aqui é que se já foi executado, provavelmente não precisa executar mais vezes pois apenas estaria consumindo processos.
*/
if ($this->nome_do_user === null) {
$this->nome_do_user = json_decode(file_get_contents('php://input'), true);
}
return $this;
}
public function getNomeUser()
{
return $this->nome_do_user;
}
}
$correios = new Correios();
var_dump($correios->getNomeUser());
In practice, it does the same as the second example. The difference is that we use the constructor method. __construct()
.
The method __construct()
is automatically invoked every time you start a new instance of a class.
That is, whenever you do $var = new NomeDaClasse()
, if there is a method called __construct()
, this will be executed automatically. See: http://php.net/manual/en/language.oop5.decon.php
Static methods
Now let’s "radicalize" and show this whole thing with static methods.
class Correios
{
private static $nome_do_user = null;
public static getNomeUser()
{
/*
Uma condicional que verifica se está vazio. Isso evita executar a rotina mais de uma vez.
A lógica aqui é que se já foi executado, provavelmente não precisa executar mais vezes pois apenas estaria consumindo processos.
*/
if (self::$nome_do_user === null) {
self::$nome_do_user = json_decode(file_get_contents('php://input'), true);
}
return self::$nome_do_user;
}
}
var_dump(Correios::getNomeUser());
When to use static methods and properties?
The thing that seemed simple goes deeper into very complex matters.
You may think it’s much cleaner visually invoking Correios::getNomeUser();
instead of
$correios = new Correios();
$correios->getNomeUser();
After all, if I can get the same result with a smaller code, this is the one I should use?
Performatically, static style is better than instantiating an object?
With each question, you will probably create new questions. So the complexity in guiding you what or which path to follow.
Final consideration
Despite the criticisms and advice in the existing responses and comments, do not be discouraged. Every good programmer started out like you, writing conceptual code without knowing what exactly you were doing. Over time, practicing hard and studying, you will be able to understand how to use properly.
I could still demonstrate 5 more different ways, but I believe that so far is enough for you to understand and choose what you really want to use. But I agree with what they quoted. Avoid using OOP mirabulant things if you don’t understand what you’re doing. In a summary, look for the basic step that is the definition of visibility (public, protected, private).
You’re not calling the method
RecebeJson
before callinggetUser
. The variable does not exist yet. If you want it to be available in the creation of the object, create a constructor(__construct(){}
) and start him on the Receive method.– user28595
I did it this way, I created a constructor and it calls Recebejson.. Then I tried to display the user via $correios->getUser();, but it still hasn’t been done yet...
– Pedro Antônio
In php the constructor is a method previously defined as
function __construct(){}
.– user28595
Yeah, just like I did....
– Pedro Antônio