When should I declare variables in a PHP class?

前端 未结 6 1021
小鲜肉
小鲜肉 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:36

    As Federico Culloca said "That variable isn't uninitialized, it's just undeclared". Also you didn't define any access modifiers for them so that they behaving like public modifier applied to them.

    You may already have known. PHP is a loosely typed language. But a programmer should always follow the best practices (but not to use primitive data types in php).

    You should use private modifier for class level variables and provide accessor and mutator methods (Getters and Setters) for them.

提交回复
热议问题