By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Long String Contstants in Class Definitions

P: n/a
Hi,

I have a question about defining long string constants in class
defintions.

For an example, let's say I have some long SQL queries that I'd like
to store in a class. Furthermore, I don't want to instantiate the
class to use them since they're just a group of constants.

I can see a couple ways to acheive this, but they all leave unreadable
code. I can:

1) put the whole string all in one line, ignoring wrapping:

class MyQueries {
var $query1 = "SELECT this FROM that WHERE blah AND blah AND
blah..."
}

2) leave the string open and close it on another line:

class MyQueries {
var $query1 = "SELECT this
FROM that
WHERE blah
AND blah
AND blah
..."
}

3) use heredoc syntax:

class MyQueries {
var $query1=<<<ENDH
SELECT this
FROM that
WHERE blah
AND blah
AND blah
...
ENDH;
}

I don't like any of these. The first and last make the source code
unreadable and the middle one includes all the whitespace and CRLF's
in the string, which I don't want.

Coming from a C background, I'd like to do this:

class MyQueries {
var $query1="SELECT this"
" FROM that"
" WHERE blah"
" AND blah"
" AND blah"
" ...";
}

But PHP doesn't seem to support that syntax (out of curiousity: why
not?), and since it's a class definition I can't use the '.' operator
to concatenate them.

Does anybody have a good solution? I'm afraid I'm just going to have
to give up on the "No instantiation" thing and just define them with
"." operators in a constructor, even though it seems unneccessary.

Thanks,
Andrew
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Andrew Palumbo" <to*****@yahoo.com> wrote in message
news:3c**************************@posting.google.c om...
Hi,

I have a question about defining long string constants in class
defintions.

For an example, let's say I have some long SQL queries that I'd like
to store in a class. Furthermore, I don't want to instantiate the
class to use them since they're just a group of constants.
<snip>
Thanks,
Andrew


This is a good use for define(), since you effectively want static
constants. These concatenate fine.

define("X","This is a string".
" that needs to be longer".
" than comfortably fits on a line.");

Can't imagine why you'd want them class-specific, but at least this works.

You could always construct them in the constructor:
class c {
var $v;

function c() {
$v="This is a string".
" that needs to be longer".
" than comfortably fits on a line.";

echo $this->v;
}
}

I hadn't realised you couldn't do this with "var" variables' declaration,
but it makes sense I suppose.

Garp

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.