Why do constants have quotes around when defined and class quoteshave no quotes?? | |
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
Constants: http://www.talkphp.com/showthread.php?t=1151
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird. | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote: Quote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
>
Constants: http://www.talkphp.com/showthread.php?t=1151
>
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
The article tells you as far as I can see.
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined"). | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote: Quote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
> Quote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
> > Quote:
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
>
The article tells you as far as I can see.
>
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").
I understand how to do it, I just don't understand why?? Why is one
defined without quotes?? | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On Dec 11, 9:29 am, adam.timberl...@gmail.com wrote: Quote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
>
>
> Quote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
> Quote: Quote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
> > Quote: Quote:
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
> Quote:
The article tells you as far as I can see.
> Quote:
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").
>
I understand how to do it, I just don't understand why?? Why is one
defined without quotes??
Because when you do this:
define('FOO', 'BAR');
you're calling a function. That function takes parameters (namely a
string and something else). Literal strings must be quoted.
In the case of a class constant:
class foo {
const BAR = 'BAZ';
}
you're not calling a function -- it's a language construct. The fact
that you don't put quotes around the BAR is simply the way the
designers of PHP chose to implement it. | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
<adam.timberlake@gmail.comwrote in message
news:930cd6cc-26ab-4dbd-a622-414205543801@18g2000hsf.googlegroups.com... Quote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
>
Constants: http://www.talkphp.com/showthread.php?t=1151
>
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
DON'T MULTI-POST !!!
cross-post if you must, but DON'T multi-post. | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
adam.timberlake wrote: Quote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I was
reading the article below and wondered why are normal constants set with
quotes around them, while "class constants" (those little things I
learned yesterday from the article) don't have to have quotes around the
constants when you define them?
You don't *have* to put quotes around a constant when you define it. e.g.
define('FOOBAR', 'Hello world');
define(FOOBAR, 'Hello world');
are both equivalent. However, the latter will raise an E_NOTICE error.
(Most people tend to suppress E_NOTICE errors anyway as they're a nuisance
and are raised any time anyone ever tries to do anything interesting.)
OK. They're not exactly equivalent. The exact difference in semantics can
be spotted when you try to define the same constant twice...
<?php
error_reporting(E_ALL);
define(BAR, 'BAZ');
define(BAR, 'hello');
echo BAZ . "\n";
?>
versus
<?php
error_reporting(E_ALL);
define('BAR', 'BAZ');
define('BAR', 'hello');
echo BAZ . "\n";
?>
But that's an edge case which you're not likely to have to worry about.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 2:04.]
Sharing Music with Apple iTunes http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/ | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
"Toby A Inkster" <usenet200712@tobyinkster.co.ukwrote in message
news:p8n235-0h7.ln1@ophelia.g5n.co.uk... Quote:
adam.timberlake wrote:
> Quote:
>This is a really basic question for all you people out there who know
>PHP. This is not a problem but just something I'm confused about. I was
>reading the article below and wondered why are normal constants set with
>quotes around them, while "class constants" (those little things I
>learned yesterday from the article) don't have to have quotes around the
>constants when you define them?
>
You don't *have* to put quotes around a constant when you define it. e.g.
>
define('FOOBAR', 'Hello world');
define(FOOBAR, 'Hello world');
>
are both equivalent. However, the latter will raise an E_NOTICE error.
(Most people tend to suppress E_NOTICE errors anyway as they're a nuisance
and are raised any time anyone ever tries to do anything interesting.)
>
OK. They're not exactly equivalent. The exact difference in semantics can
be spotted when you try to define the same constant twice...
>
<?php
error_reporting(E_ALL);
define(BAR, 'BAZ');
define(BAR, 'hello');
echo BAZ . "\n";
?>
>
versus
>
<?php
error_reporting(E_ALL);
define('BAR', 'BAZ');
define('BAR', 'hello');
echo BAZ . "\n";
?>
>
But that's an edge case which you're not likely to have to worry about.
toby! that's dangerous thinking. DON'T put yourself in a position to
experience bugs by writting them in! it doesn't matter how remote the
possability is. imagine the amount of hours you'll spend looking for this
bug when the nuance that causes it is a pair of tics. i'd be pissed enough
to walk down to the programmer who did it and slap them on the back of the
head when i finally did! | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote: Quote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
>
>
> Quote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
> Quote: Quote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
> > Quote: Quote:
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
> Quote:
The article tells you as far as I can see.
> Quote:
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").
>
I understand how to do it, I just don't understand why?? Why is one
defined without quotes??
You asked "Could someome please explain to me the reasons why there
are 2 ways to
define them - one via a function, and one via,"
I pointed out the answer to that question! | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On Tue, 11 Dec 2007 17:35:15 +0100, Steve <no.one@example.comwrote: Quote:
"Toby A Inkster" <usenet200712@tobyinkster.co.ukwrote in message
news:p8n235-0h7.ln1@ophelia.g5n.co.uk... Quote:
>You don't *have* to put quotes around a constant when you define it.
>e.g.
>>
>define('FOOBAR', 'Hello world');
>define(FOOBAR, 'Hello world');
>>
>are both equivalent. However, the latter will raise an E_NOTICE error.
>(Most people tend to suppress E_NOTICE errors anyway as they're a
>nuisance
>and are raised any time anyone ever tries to do anything interesting.)
>>
>OK. They're not exactly equivalent.
>>
><?php
>error_reporting(E_ALL);
>define(BAR, 'BAZ');
>define(BAR, 'hello');
>echo BAZ . "\n";
>?>
>>
>versus
>>
><?php
>error_reporting(E_ALL);
>define('BAR', 'BAZ');
>define('BAR', 'hello');
>echo BAZ . "\n";
>?>
>>
>But that's an edge case which you're not likely to have to worry about.
>
toby! that's dangerous thinking. DON'T put yourself in a position to
experience bugs by writting them in! it doesn't matter how remote the
possability is. imagine the amount of hours you'll spend looking for this
bug when the nuance that causes it is a pair of tics. i'd be pissed
enough
to walk down to the programmer who did it and slap them on the back of
the
head when i finally did!
Hear, hear.
I always enable full error_reporting on development BTW, and every one of
them has to go... (well, if it's not code in an external library).
--
Rik Wasmus | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On Tue, 11 Dec 2007 17:39:29 +0100, Captain Paralytic
<paul_lautman@yahoo.comwrote: Quote:
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote: Quote:
>On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote: Quote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have
>quotes Quote:
around the constants when you define them? It seems really weird to
>me Quote:
why PHP would do it this way??
>> >> Quote:
Could someome please explain to me the reasons why there are 2 ways
>to Quote:
define them - one via a function, and one via, what is it? A
>language Quote:
construct? I don't know. The latter having no quotes around to
define!! Really weird.
>> Quote:
The article tells you as far as I can see.
>> Quote:
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").
>>
>I understand how to do it, I just don't understand why?? Why is one
>defined without quotes??
>
You asked "Could someome please explain to me the reasons why there
are 2 ways to
define them - one via a function, and one via,"
>
I pointed out the answer to that question!
Indeed, to put it more explicitly:
- after the const keyword the next string cannot be anything else then the
name of the constant.
- the first argument of the define function can be literel strings, other
constants, expression results, etc.
--
Rik Wasmus | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On Dec 11, 4:43 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote: Quote:
On Tue, 11 Dec 2007 17:39:29 +0100, Captain Paralytic
>
>
>
<paul_laut...@yahoo.comwrote: Quote:
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote: Quote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have
quotes
around the constants when you define them? It seems really weird to
me
why PHP would do it this way??
> > Quote: Quote:
Could someome please explain to me the reasons why there are 2 ways
to
define them - one via a function, and one via, what is it? A
language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
> Quote: Quote:
The article tells you as far as I can see.
> Quote: Quote:
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").
> Quote: Quote:
I understand how to do it, I just don't understand why?? Why is one
defined without quotes??
> Quote:
You asked "Could someome please explain to me the reasons why there
are 2 ways to
define them - one via a function, and one via,"
> Quote:
I pointed out the answer to that question!
>
Indeed, to put it more explicitly:
- after the const keyword the next string cannot be anything else then the
name of the constant.
- the first argument of the define function can be literel strings, other
constants, expression results, etc.
--
Rik Wasmus
How do I multi-post? | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On Tue, 11 Dec 2007 21:40:07 +0100, <adam.timberlake@gmail.comwrote: Quote:
How do I multi-post?
First of all, you wanted to reply to Steve, not me.
Secondly, multi-posting is a lesser evil then cross-posting(well sometimes
it's OK, depends on the subject), normally you stick with just on on-topic
group.
Finally: this is usenet, I have no idea how you'd multipost with Google
Groups, but it seems to work for some people. I'd advise you to get a real
newsreader (/access to a news server, possible supplied by your ISP, there
are free ones around) to reap the full benefits of usenet.
--
Rik Wasmus | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
Rik Wasmus wrote: Quote:
Secondly, multi-posting is a lesser evil then cross-posting(well
sometimes it's OK, depends on the subject), normally you stick with just
on on-topic group.
Quite the opposite! Multi-posting is sending several copies of the same
(or very similar) messages to different groups. This is considered
annoying because someone might help out, giving you a long and thoughtful
answer, and spending a lot of time giving you help, without knowing that
someone has already answered your question in another group.
Cross-posting sends a single copy of the message to multiple groups (like
CCing in e-mail). Replies tend to go to all the groups, so everyone can
see the whole conversation. To cross-post, you normally just type the
names of multiple groups into your "Newsgroups" field, separated by
commas.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 8:56.]
Sharing Music with Apple iTunes http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/ | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
"Toby A Inkster" <usenet200712@tobyinkster.co.ukwrote in message
news:icf335-l8g.ln1@ophelia.g5n.co.uk... Quote:
Rik Wasmus wrote:
> Quote:
>Secondly, multi-posting is a lesser evil then cross-posting(well
>sometimes it's OK, depends on the subject), normally you stick with just
>on on-topic group.
>
Quite the opposite! Multi-posting is sending several copies of the same
(or very similar) messages to different groups. This is considered
annoying because someone might help out, giving you a long and thoughtful
answer, and spending a lot of time giving you help, without knowing that
someone has already answered your question in another group.
>
Cross-posting sends a single copy of the message to multiple groups (like
CCing in e-mail). Replies tend to go to all the groups, so everyone can
see the whole conversation. To cross-post, you normally just type the
names of multiple groups into your "Newsgroups" field, separated by
commas.
bingo. | | | | re: Why do constants have quotes around when defined and class quoteshave no quotes??
On Tue, 11 Dec 2007 23:25:54 +0100, Toby A Inkster
<usenet200712@tobyinkster.co.ukwrote: Quote:
Rik Wasmus wrote:
> Quote:
>Secondly, multi-posting is a lesser evil then cross-posting(well
>sometimes it's OK, depends on the subject), normally you stick with just
>on on-topic group.
>
Quite the opposite! Multi-posting is sending several copies of the same
(or very similar) messages to different groups. This is considered
annoying because someone might help out, giving you a long and thoughtful
answer, and spending a lot of time giving you help, without knowing that
someone has already answered your question in another group.
>
Cross-posting sends a single copy of the message to multiple groups (like
CCing in e-mail). Replies tend to go to all the groups, so everyone can
see the whole conversation. To cross-post, you normally just type the
names of multiple groups into your "Newsgroups" field, separated by
commas.
Blah, offcourse I meant 'cross-posting is a lesser evil then
multi-posting'. For some weird reason I always get the terminology mixed
up...
--
Rik Wasmus |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|