tags:

views:

387

answers:

5

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

class someThing {

    private $cat;
    private $dog;
    private $mouse;
    private $hamster;
    private $zebra;
    private $lion;

    //getters, setters and other methods
}

But I've also seen people using a single array to store all the variables:

class someThing {

    private $data = array();

    //getters, setters and other methods
}

Which do you use, and why? What are the advantages and disadvantages of each?

+6  A: 

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.

Greg
+1  A: 

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.

tkotitan
+5  A: 

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.

Peter Bailey
A: 

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.

Mainegreen
A: 
  • 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.

OIS