share private value with another attribute in the same class

Asked

Viewed 19 times

0

I have the following model class below:

class modelo {
  private $variavel1 = "Algum";
  private $variavel2 = $this->variavel1." Valor";
  .....
}

How to attach the value of variable 1 at value variable2 both being of the same class?

Obs.: NAY would like to receive this value by the constructor method or methods (magical or not)!

Is there any way?

  • You can do this in the constructor (without receiving the same value). Outside of a method this is impossible.

  • 1

    You are using OO, use get/set. At get of variable 2, returns the value of 1.

  • It is. I would NOT like to use either the constructor or magic methods. So it must be hopeless anyway! I thought because they were both attributes within the same class there would be a resource for that!

  • Why don’t you want to use the constructor to initialize the fields?

  • because the class is big and I would not like to write the same attributes twice, (declaration and constructor). I think it would be cleaner for the class. Since it is impossible to retrieve a private from outside the class then from inside it should be possible

No answers

Browser other questions tagged

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