Hello Ashely,

I would initialize the variable when I'm defining it as there isn't much of a point of doing it in the constructor unless I'm having the value changed by a parameter.

In my opinion:

class House
    public $roof = true;

is the way to go.


On 15/04/2010 11:54, Ashley Sheridan wrote:
On Thu, 2010-04-15 at 07:42 -0700, Tommy Pham wrote:

Hi Ashley,

-----Original Message-----
From: Ashley Sheridan [mailto:a...@ashleysheridan.co.uk]
Sent: Thursday, April 15, 2010 6:38 AM
To: PHP General List
Subject: [PHP] class attributes and __construct

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
likely remain at a default value and only ever change in a few rare

For example:

class Person
     public $right_handed = true;
I recommend setting it to private or protected instead of public to protect the 
integrity of the app.  And add a get method/function to obtain the value.

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

     function set_hand($side)
         if($side == 'left'
             $this->right_handed = false;
             $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
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
other? Is there a rule of thumb as to what belongs in __construct and
what does not?


As for setting the default value in the construct, I recommend not to because 
should PHP support overloading later, you can then have another method/function 
to change its non-default value along with the initial parameters for the 
class.  I use the constructor to set initial parameters for the class or 
initialize any class specific settings such as connection for DBAL.


Maybe my example wasn't the best, but I did mean that my public variable
there was a value that should be initially set with the class
initialisation I realise the differences between public, private and
protected variables, that wasn't my question. What I wanted to know was
if there was a convention about what specific properties should be set
through the public $var = method and what should be left for the
__construct function?

For example, which would be better here (assuming a variable number of
variables that might be set this way:

class House
     public $roof = true;

class House
     function __construct()
         $this->roof = true;

Aside from the amount of typing (which isn't a serious consideration for
me anyway) and assuming that such variable initialisation will not rely
on variable input but will be fixed with the option of a class method to
change the value later, what would be the preferred method and are there
any caveats I'm unaware of right now (as I'm aware of none so far) to
either method?


Reply via email to