When should I declare variables in a PHP class?

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

    That variable isn't uninitialized, it's just undeclared.

    Declaring variables in a class definition is a point of style for readability. Plus you can set accessibility (private or public).

    Anyway, declaring variables explicitly has nothing to do with OOP, it's programming-language-specific. In Java you can't do that because variables must be declared explicitly.

    0 讨论(0)
  • 2020-11-30 03:26

    In general variables should be initialized as soon as you have enough info to do it properly.

    If a class variable needs certain info to be sensibly initialized then that info should be passed to the constructor.

    Using PHP's syntax to implicitly declare variables at the point of definition is, IMHO a surefire way to introduce bugs - if your class needs a variable then declare it, and use all of the information hiding that OOP affords you.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 03:37

    If you declare a member inside the class you can set its accessibility e.g

    private $varname;
    
    0 讨论(0)
  • 2020-11-30 03:41

    You should always declare your member variables and specify their accessibility within your classes. I like to put this information at the end of the class after my functions.

    You should define them as soon as you have enough information to do so. Possibly in the constructor or via setter functions.

    It is important to do this because it makes life much easier for people working with your code. They don't have to guess where different properties are coming from or why they're there. Also, most (if not all) IDEs will not pick up on class variables unless you've declared them somewhere. Code completion/hints are one of the many benefits of IDEs and without declaring your variables, you will render that functionality useless.

    0 讨论(0)
提交回复
热议问题