RE: [PHP] class attributes and __construct

2010-04-15 Thread Tommy Pham
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
 might
 likely remain at a default value and only ever change in a few rare
 circumstances?
 
 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;
 }
 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
 

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.

Regards,
Tommy


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] class attributes and __construct

2010-04-15 Thread Ashley Sheridan
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
  might
  likely remain at a default value and only ever change in a few rare
  circumstances?
  
  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;
  }
  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
  
 
 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.
 
 Regards,
 Tommy
 


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?

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




Re: [PHP] class attributes and __construct

2010-04-15 Thread Fernando

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.

Fernando.

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

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;
 }
 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

   

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.

Regards,
Tommy

 


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?

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



   


RE: [PHP] class attributes and __construct

2010-04-15 Thread Tommy Pham
 -Original Message-
 From: Fernando [mailto:ferna...@ggtours.ca]
 Sent: Thursday, April 15, 2010 10:24 AM
 To: php-general@lists.php.net
 Subject: Re: [PHP] class attributes and __construct
 
 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.
 
 Fernando.

That's what I meant.  IMO, it's also a micro optimization since it doesn't need 
an extra step of looking the variable and assigning the value when it's not 
needed.  Plus, it allows better polymorphism through methods/functions.

Regards,
Tommy

 
 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
  might
  likely remain at a default value and only ever change in a few rare
  circumstances?
 
  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;
   }
   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
 
 
  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.
 
  Regards,
  Tommy
 
 
 
  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?
 
  Thanks,
  Ash
  http://www.ashleysheridan.co.uk
 
 
 
 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] class attributes and __construct

2010-04-15 Thread Larry Garfield
On Thursday 15 April 2010 08:37:40 am Ashley Sheridan wrote:

 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

I tend to favor setting the default with the property itself rather than in 
the constructor, even if I expect the constructor to specify it.  I really 
really hate having undefined variables. :-)  So even if, for instance, I'm 
going to pass in a db connection object in the constructor I will define it in 
the class and set it to NULL rather than not setting to anything.

OK, I'm a little OCD, but it works. :-)  

--Larry Garfield

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php