Every class must have a builder

Asked

Viewed 944 times

3

I know it is a question of beginner, but I need to remedy this doubt. I searched through the internet and not always the sites enter in "agreement".

Every class must have a builder ?

Example:

I can do the student class and have no constructor and the methods register (name, age, rm) and this method is responsible for providing the values for the class properties (name, age, rm) and the method disable(id_student).

In my view this is more practical at the time of instantiating the class. If it is just disable the student just called desativar().

Because if you do the constructor receiving class properties every time you access some method of it I will have to pass all values (name, age, rm), even if only desativar().

I’m thinking wrong? what’s the best practice?

  • I was so big to answer, I even forgot to check if I already had the answer. I have to agree that it is the same question, practically

2 answers

4


Every class must have a builder?

No. In PHP, classes can be declared without the definition of a constructor. However:

"Classes that have a constructor method call this method each time a new object is created, so it is appropriate for any initialization that the object may need before it is used." PHP: Constructors and Destructors

Therefore, it is recommended that you use the constructor to initialize attributes that are bound in common with all class methods.

If the attribute is method specific, initialize it at the time of calling the method itself.

(...) For if the builder gets the properties of the whole class moment I access any method of it I will have to pass all the values (name, age, rm), even if only to deactivate().

I didn’t fully understand your statement. It is important to understand that whenever a class is instantiated with constructor initialized attributes, these properties will be available for any object method until it is destroyed.

  • In my " statement" I mean when there are several students registered in the database. These are displayed in a list. Then I choose a student to deactivate. But if the student class has the attributes in the constructor every time I have to pass these values, then I do not know is I see no logic to have to pass these values at all times

2

No, you don’t have to.

The constructor simply serves to initialize class attributes.

The class stdClass for example does not have the method __construct.

In some cases, it is better to have a constructor than to have to use methods of the type mutator and accessor.

Example with constructor:

class Aluno
{
   protected $nome;

   protected $idade;

   public function __construct($nome, $idade)
   {
       $this->nome = $nome; 

       $this->idade = $idade;
   }
}

Example without constructor:

class Aluno
{
   protected $nome;
   protected $idade;

   public function setNome($nome)
   {
      $this->nome = $nome;

   }

   public function setIdade($idade)
   {
      $this->idade = $idade;
   }

}

Browser other questions tagged

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