473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

array as “class constant”

Dormilich
8,658 Expert Mod 8TB
Hi,

I’d like to have something like
Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.     const HASH = array(…);
  4. }
but that’s not allowed, so my question is, should I use a standard or static property instead?
Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.     private static $HASH = array(…);
  4. // or
  5.     private $HASH = array(…);
  6. }
Jan 19 '10 #1
8 4063
aktar
105 100+
I don't know of any other options than to use either a standard or static property
Jan 19 '10 #2
Dormilich
8,658 Expert Mod 8TB
and which would you prefer, and why?
Jan 19 '10 #3
Markus
6,050 Expert 4TB
Perhaps if you describe your requirement we could offer some advice :)
Jan 19 '10 #4
Dormilich
8,658 Expert Mod 8TB
ok (though it won’t make much difference)

initially I wanted a constant (= unchangeable) array in the class, to use it in one of the methods. const HASH = array(…); would have been perfect for that purpose, but it’s not allowed, leaving me with the choice of either using protected $HASH = array(…); or protected static $HASH = array(…);. now I’d like to know, which one would be better.
Jan 19 '10 #5
dlite922
1,584 Expert 1GB
serialize your array and put it in a constant :)

Yes that means you can still change it when you unserialize() it. :)

These are the odd cases where PHP does not come up to par with C++.

Use static just so you know you should behave differently while using it, and hopefully to remember to not change it.

Explain your situation a little bit more and I might suggest something else.



Dan
Jan 19 '10 #6
Dormilich
8,658 Expert Mod 8TB
serialize your array and put it in a constant :)
very funny, indeed.

I never though such a simple question can get so complicated.
Attached Files
File Type: zip class.headers.php.zip (4.8 KB, 81 views)
Jan 19 '10 #7
dlite922
1,584 Expert 1GB
The way you have it is how I'd do it.

If I was hell-bent on making sure it did not change, I'd make a singleton class and make all the error-codes in there constants and its 2 methods static.

So then I'd use it like this for your deny() function:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(HTTP_Headers::isValid($code))
  3.  
  4. header("HTTP/1.1 $code " . HTTP_Headers::get([$code]));
  5.  
  6.  
Dan,
[the php nut @]
Jan 19 '10 #8
Dormilich
8,658 Expert Mod 8TB
firstly, Abstract Registry is better than Singleton.

secondly, too much computing for that simple purpose.
Jan 19 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Kyle | last post by:
Hi, Is it possible to initialize a constant memeber array in a class? I tried several syntax but all failed. class A { public: A(); ~A(); private:
7
by: Rade | last post by:
Please have a look at the following program: #include <iostream> template <const int array, size_t index> class ArrayIndex { public: static const int value = array; };
3
by: Mark Dunmill | last post by:
I can't create a Constant/Read-only array field in managed C++ classes - doesn't allow the keyword const pointer to const object on array fields in managed C++ classes. e.g. Want to define a...
3
by: danbraund | last post by:
Hi everyone, I'm a long time C coder, who is coding his final year project in C++ to run under the MIT click routing system. Being fairly new to the OO side of the language, my problem is this: ...
22
by: howachen | last post by:
which one is recommend? seems they perform the same thing... regards, howa
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
9
by: barcaroller | last post by:
Can variables be used for array size in C++? I know that in the past, I could not do the following: foo (int x) { type arr; } I have recently seen code that does exactly that. Is it right?
3
by: longbrmb | last post by:
I'm new to C++/CLI and my main background is Java. I'm trying to create an array of constants as a static member of a class. An example is shown below: public ref class TestClass { public:...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.