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.

Relevant Pages

  • Re: attempting to return values from array from w/in a function
    ... What I get is an array w/ only the most recent array entry. ... I am new to PHP & do not have formal scripting ... you end up comparing strings, but the test you mean probably is simply: ... case 1: {codeblock} ...
  • Re: jtable change values problem
    ... > I have a jTable that is populated with an array of strings. ... it is better to extend DefaultTableModel and just override what needs to be ... You can then use a constructor to set the data you want, ...
  • How does PHP store strings?
    ... I've read in numerous places that PHP stores strings as an array of ... higher concept of an array so it ... seems to me that PHP strings do not require the null character at the ...
  • Re: Help with php array loading/retrieval ???
    ... The problem I am having is referencing the array elements as strings ... but it appears in reality its being references as a vector/index. ... As much as I've used PHP over the years I usually ...
  • Re: Query string before parsing
    ... which PHP does not handle. ... PHP has a precedent for introducing breaking changes to the language. ... This convention is a little hairy for forms that may have zero, one, or many checkboxes checked, as the handler needs to check whether the form value is a string or an array of strings, but it'd work and PHP has tools for doing that. ...