class attributes and __construct

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I think this is probably going to end up as one of those coders'
preference type of things, but I was wondering what was considered the
general best approach.

When creating a class, you can define default values for the object in
the class itself, and within the __construct function. Now, while I see
the advantage to using __construct to set properties that might depend
on a variable, what would be the best approach for any values that might
likely remain at a default value and only ever change in a few rare
circumstances?

For example:

class Person
{
    public $right_handed = true;

    function __construct($name, $height)
    {
        $this->name = $name;
        $this-height = $height;
    }

    function set_hand($side)
    {
        if($side == 'left'
        {
            $this->right_handed = false;
        }
        else
        {
            $this->right_handed = true;
        }
    }

}

Now, this is a simple example, but a value like $right_handed should
only ever change if it's not the typical. As most people are
right-handed it would make sense to set it to true and allow it to be
changed to false as necessary. What I'm wonder is, where is the best
place to set it to true, in the list of class properties at the top, or
in the __construct() function?

I know I could move it to __construct and give it a default value in the
arguments list, but that brings it's own problems. What if the argument
list grows too big, and which attribute would be deemed more important
than another that you might want to override it without specifying every
other? Is there a rule of thumb as to what belongs in __construct and
what does not?

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux