When using __constructor magic method or set and get

Asked

Viewed 1,230 times

6

My doubt is with constructor relation, for example, I have a class with name, age. the correct is to use __construct to pass values to them or use set e get ?

1 answer

4


First you have to answer if having this data initialized in the class instance is fundamental to consider it consistent from the start.

The constructor’s function is to create an instance in a valid state from the start, that is, do not allow access until all important data is initialized.

I will believe that in this case these two data are necessary and therefore must be initialized by the constructor.

Still nothing prevents you from having setters and getters for them also, mainly getter that does something different from what the builder does. Just like having Setter nothing prevents using the property relating to it in the constructor, even if it is not fundamental. It is more common to put in the constructors the essential properties for the class but there are also cases that one can put extra properties in the constructor to facilitate the life of the class user programmer.

But if you search here, they should not always be used.

  • if I pass the data at the time of class instantiation, I can avoid using set if I want, this ?

  • 1

    It can, of course, if this data does not need to be changed later, not only can it avoid its use.

  • One more question. Can I pass function of the class itself in the constructor ? ex public Function ____Construct($name, general()), it is allowed to do this ?

  • You cannot because the constructor declaration or any function or method only allows you to have parameters. You cannot execute anything. But you can do it within the builder’s body under certain circumstances. Sometimes I get confused with what I can in each language, I don’t remember well about PHP but I think I could only call static methods of the class, after all if I call a method that depends on the initialized instance which has not yet happened inside the constructor. I just can’t guarantee because PHP does something crazy that nobody can explain. Static method wouldn’t stop it. Test and see for yourself.

  • OK, thank you very much. I asked this because I have an attribute that needs to be instantiated in the constructor, a unique id generator, so I’m trying to solve this.

  • but I can within the constructor call a method that returns a value for an attribute, without reference to it in the parameters ?

  • ex: ... ' __Construct($val){ $this->atr1 = $val; $this->atr2 = geraValor(); } '

  • 1

    As I told you geraValor() is static, can. Or if it is external to this class. But also as I said, I am based on decent languages. As PHP is the language mainstream more poorly planned than I have ever seen, it may even allow him to call anything indiscriminately by throwing away the constructor’s advantage which is not to allow access to the instance until it is complete.

  • blz, understand. thanks again

  • When in doubt, test. When you have problem, put here. If you think you have error even working, also.

  • I tested and it worked, but I was wondering if this is correct

Show 6 more comments

Browser other questions tagged

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