473,405 Members | 2,279 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,405 software developers and data experts.

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.
Dec 11 '07 #1
14 1887
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
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").
Dec 11 '07 #2
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
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").
I understand how to do it, I just don't understand why?? Why is one
defined without quotes??
Dec 11 '07 #3
On Dec 11, 9:29 am, adam.timberl...@gmail.com wrote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
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").

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.
Dec 11 '07 #4

<ad*************@gmail.comwrote in message
news:93**********************************@18g2000h sf.googlegroups.com...
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.
Dec 11 '07 #5
adam.timberlake wrote:
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/
Dec 11 '07 #6

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:p8************@ophelia.g5n.co.uk...
adam.timberlake wrote:
>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!
Dec 11 '07 #7
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
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").

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!
Dec 11 '07 #8
On Tue, 11 Dec 2007 17:35:15 +0100, Steve <no****@example.comwrote:
"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:p8************@ophelia.g5n.co.uk...
>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
Dec 11 '07 #9
On Tue, 11 Dec 2007 17:39:29 +0100, Captain Paralytic
<pa**********@yahoo.comwrote:
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote:
>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??
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").

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
Dec 11 '07 #10
On Dec 11, 4:43 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Tue, 11 Dec 2007 17:39:29 +0100, Captain Paralytic

<paul_laut...@yahoo.comwrote:
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote:
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??
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").
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
How do I multi-post?
Dec 11 '07 #11
On Tue, 11 Dec 2007 21:40:07 +0100, <ad*************@gmail.comwrote:
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
Dec 11 '07 #12
Rik Wasmus wrote:
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/
Dec 11 '07 #13

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:ic************@ophelia.g5n.co.uk...
Rik Wasmus wrote:
>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.
Dec 12 '07 #14
On Tue, 11 Dec 2007 23:25:54 +0100, Toby A Inkster
<us**********@tobyinkster.co.ukwrote:
Rik Wasmus wrote:
>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
Dec 12 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
14
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
1
by: chris han | last post by:
Hi, all, I'm trying to use Conditional Compilation Statements in my code. using System; #define DEBUG public class MyClass { public static void Main() {
23
by: Akhil | last post by:
Since a character constant is an int value represented as a character in single quotes,so it is treated as a 1 byte integer now look at the following snippet. #include<stdio.h> int main(void)...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.