What's the best way to store class variables in PHP?

前端 未结 5 2426
旧巷少年郎
旧巷少年郎 2021-02-20 11:08

I currently have my PHP class variables set up like this:

class someThing {

    private $cat;
    private $dog;
    private $mouse;
    private $hamster;
    pr         


        
5条回答
  •  温柔的废话
    2021-02-20 12:12

    I prefer the first method, for a few reasons:

    In a good IDE, the class properties show up, even if private/protected It's easier to see what has already been defined, reducing the chance you store the same information twice. If the proverbial bus hits you on the way home, it's a lot simpler for another developer to come in and read your code. And while it doesn't apply to private var, it does to protected vars, in classes that extend this class, you really should try to avoid the second method for pure readability.

    Also, as a side note, I almost always choose protected over private unless I have a very specific reason to make it private.

    The only time I'd probably use the second method was if I was storing a collection of many of one kind of thing.

提交回复
热议问题