how to declare a class array constant

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

how to declare a class array constant

Post 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]
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

Constants can only have scalar data (boolean, integer, float and string).
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

What is the rationale

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: What is the rationale

Post 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.
Post Reply