class attributes and __construct
class attributes and __construct
am 15.04.2010 15:37:40 von Ashley Sheridan
--=-Oc9uNJI2+J3H8M2XDhIW
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
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
--=-Oc9uNJI2+J3H8M2XDhIW--
RE: class attributes and __construct
am 15.04.2010 16:42:21 von Tommy Pham
Hi Ashley,
> -----Original Message-----
> From: Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]
> Sent: Thursday, April 15, 2010 6:38 AM
> To: PHP General List
> Subject: [PHP] class attributes and __construct
>=20
> 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.
>=20
> 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?
>=20
> For example:
>=20
> class Person
> {
> public $right_handed =3D 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 =3D $name;
> $this-height =3D $height;
> }
>=20
> function set_hand($side)
> {
> if($side == 'left'
> {
> $this->right_handed =3D false;
> }
> else
> {
> $this->right_handed =3D true;
> }
> }
>=20
> }
>=20
> 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?
>=20
> 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?
>=20
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>=20
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: class attributes and __construct
am 15.04.2010 17:54:18 von Ashley Sheridan
--=-rO9eyysmsB1pZvk/ZjSN
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Thu, 2010-04-15 at 07:42 -0700, Tommy Pham wrote:
> Hi Ashley,
>
> > -----Original Message-----
> > From: Ashley Sheridan [mailto:ash@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
--=-rO9eyysmsB1pZvk/ZjSN--
Re: class attributes and __construct
am 15.04.2010 19:24:18 von Fernando
--------------060204050606040908070203
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
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:ash@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
>
>
>
>
--------------060204050606040908070203--
RE: class attributes and __construct
am 15.04.2010 19:31:49 von Tommy Pham
> -----Original Message-----
> From: Fernando [mailto:fernando@ggtours.ca]
> Sent: Thursday, April 15, 2010 10:24 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] class attributes and __construct
>=20
> Hello Ashely,
>=20
> 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.
>=20
> In my opinion:
>=20
> class House
> {
> public $roof =3D true;
> }
>=20
> is the way to go.
>=20
> 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
>=20
> 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:ash@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 =3D 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 =3D $name;
> >>> $this-height =3D $height;
> >>> }
> >>>
> >>> function set_hand($side)
> >>> {
> >>> if($side == 'left'
> >>> {
> >>> $this->right_handed =3D false;
> >>> }
> >>> else
> >>> {
> >>> $this->right_handed =3D 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 =3D 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 =3D true;
> > }
> >
> > class House
> > {
> > function __construct()
> > {
> > $this->roof =3D 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: class attributes and __construct
am 16.04.2010 04:52:14 von 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