When should I declare variables in a PHP class?

前端 未结 6 1024
小鲜肉
小鲜肉 2020-11-30 03:06

I\'m new to the OOP paradigm, so there\'s probably a simple explanation for this question...

Do you always need to declare public object-wide variables in a class? F

6条回答
  •  伪装坚强ぢ
    2020-11-30 03:30

    General OOP paradigm of encapsulation says you should not expose your inner state variables out side that means they should be private, that allows you to change an implementation of your class without need to change the code where you make use of it. It's better practice to initialize variables via constructors and getters and setters method of the class.

提交回复
热议问题