Re: Split Strings Inside of Array Class/Instance Variables Not Parsing

h3xx wrote:
On Oct 27, 4:21 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
h3xx wrote:
I think I might have found a bug in PHP parsing.
Say you want to define an instance variable as an array, and say you
want to glue some strings together inside the array, whether for
formatting or inserting constants:
define('MYCONST', 'two');
class Foo {
public static $class_array = array(
MYCONST . ' three',
Looks like it would be perfectly legal, but it gives an error:
Parse error: syntax error, unexpected '.', expecting ')' in ... on
line 5
Are there other people who have come across this, and if so, have they
reported it to the dev team?
My info:
System: Linux
PHP: 5.2.11
No, it is not an error. When initializing members of a class, you must
use a static value. Your value includes the concatenation operator, and
is not static.

MYCONST or 'three' would be OK, though.

> I think you mean constant, and not static.
> You seem to be right; that IS what's making it complain. I tried using
> double quotes and interpolating a global variable, but it complained
> too.
> But the following syntax gives the same exact error:
> class Foo {
> public static $bar = array(
> 'two' . ' three',
> );
> }
> Now tell me what's not constant about gluing two (constant) strings
> together for formatting purposes, other than the fact that PHP
> internals--which we shouldn't have to worry about--need to create a
> new string in memory and copy the contents of both of them into it? I
> still think this is an error/instance of PHP being too picky.
> I guess that since the dev team is probably not going to get this
> fixed, I have another question:
> Since this is a class variable, is there some way to have it create
> the variable in a static way? I mean, in Java, there is the "static
> {}" block as a sort of "class constructor" to perform initialization
> of class variables when the class is loaded, without needing a "create-
> if-not-created" procedure in the constructors. Is there something
> similar in PHP?

(top posting fixed)

Yes, I meant constant - poor editing on my part. Sorry.

And using the concat operator is NOT a constant. It is a dynamic operation.

If you can't initialize it to a real constant, then you must use the constructor (yes, you can construct consts in a constructor). Alternatively, a static getter function can initialize is (it IS private, isn't it?).

P.S. Please don't top post. Thanks.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.