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

前端 未结 5 2493
旧巷少年郎
旧巷少年郎 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 11:52

    If code is repetitive, arrays and (foreach) loops neaten things. You need to decide if the "animal" concept in your code is repetitive or not, or if the code needs to dig in to the uniqueness of each member.

    If I have to repeat myself more than once, I loop.

    0 讨论(0)
  • 2021-02-20 11:55

    Generally, the first is better for reasons other people have stated here already.

    However, if you need to store data on a class privately, but the footprint of data members is unknown, you'll often see your 2nd example combined with __get() __set() hooks to hide that they're being stored privately.

    class someThing {
    
        private $data = array();
    
        public function __get( $property )
        {
            if ( isset( $this->data[$property] ) )
            {
                return $this->data[$property];
            }
            return null;
        }
    
        public function __set( $property, $value )
        {
            $this->data[$property] = $value;
        }
    }
    

    Then, objects of this class can be used like an instance of stdClass, only none of the members you set are actually public

    $o = new someThing()
    $o->cow = 'moo';
    $o->dog = 'woof';
    // etc
    

    This technique has its uses, but be aware that __get() and __set() are on the order of 10-12 times slower than setting public properties directly.

    0 讨论(0)
  • 2021-02-20 11:56
    • Use the first method when you know you need that variable.
    • Use the second method (an array collection of variables) when you have dynamic variable needs.

    You can combine these 2 methods, so some variables are hardcoded into your class, while others are dynamic. The hardcoded variables will have preference compared with magic methods.

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

    0 讨论(0)
  • 2021-02-20 12:14

    If you're using private $data; you've just got an impenetrable blob of data there... Explicitly stating them will make your life much easier if you're figuring out how a class works.

    Another consideration is if you use an IDE with autocomplete - that's not going to work with the 2nd method.

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