Page 1 of 1

how to declare a class array constant

Posted: Tue Oct 30, 2007 10:33 am
by yacahuma
I dont seem to be able to declare a class array constant. I want to be able to do myclass::propertyTypes. What is the right syntax? or is it not possible?

Code: Select all

[php]
class myclass
{

  const $propertyTypes = array(
      'S'=>'Single Family',
      'C'=>'Condominium',
      'P'=>'Planned unit development',
      '2'=>'2 to 4 family',
      'M'=>'Multi Family (5 or more)',
      'D'=>'deMinimus PUD',
      'O'=>'Other',
      'A'=>'Co-operative apartment'
 );

}
[/php]

Posted: Tue Oct 30, 2007 10:41 am
by seppo0010
Constants can only have scalar data (boolean, integer, float and string).

Posted: Tue Oct 30, 2007 10:41 am
by CoderGoblin
Not sure about class constants but I know for normal constants only scalar and null values are allowed. Scalar values being integer, float, string or boolean values. Maybe that has an effect. You could always just set it as a private variable if you are just using it within the class.

What is the rationale

Posted: Tue Oct 30, 2007 10:47 am
by yacahuma
Why limit what a constant could be? If i now the array will never change. I am wondering what is the rationale behind it.

Thank you

Re: What is the rationale

Posted: Tue Oct 30, 2007 10:51 am
by feyd
yacahuma wrote:Why limit what a constant could be? If i now the array will never change. I am wondering what is the rationale behind it.

Thank you
An array is not scalar. Scalar values need no further processing or evaluation to be formulated. For all intents and purposes they are constant. Arrays require evaluation, therefore not scalar.