473,323 Members | 1,574 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,323 software developers and data experts.

Difference between 'const' and 'define()'

What is the difference between 'const' and 'define()' ? When would I
prefer using 'const' over 'define', or vice versa? It seems if i do:

const secondsInMinute = 60;
define("secondsInMinute", 60);

Aren't these two the same thing?

- Jason

Oct 12 '06 #1
2 7959
"Jason" <ja********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
What is the difference between 'const' and 'define()' ? When would I
prefer using 'const' over 'define', or vice versa? It seems if i do:

const secondsInMinute = 60;
define("secondsInMinute", 60);

Aren't these two the same thing?
const defines a class constant, while define defines a global constant. You
use const-defined constants inside class using self::constant, not
$this->constant, and a define-defined constant in a normal, non-object shit.
When using const, the value must be a static value, not a result of a
mathematical operation or function. const is restricted in many ways
compared to define, and should be used in classes/objects
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Oct 12 '06 #2
It's all about the scope.
class BlahBlah
{
const MY_CONSTANT = 45676;
}
define('MY_CONSTANT', 76344);
echo BlahBlah::MY_CONSTANT . "\n" . MY_CONSTANT;
// returns:
// 45676
// 76344
Class constants are the closest thing to namespaces as php will get for this
version.

Jason wrote:
What is the difference between 'const' and 'define()' ? When would I
prefer using 'const' over 'define', or vice versa? It seems if i do:

const secondsInMinute = 60;
define("secondsInMinute", 60);

Aren't these two the same thing?

- Jason
Oct 12 '06 #3

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

Similar topics

2
by: Aakash Jain | last post by:
Can someone explain me the difference between the following C++ macro and the function MAX: #define MAX(a, b) (((a) > (b)) ? (a) : (b)) template <class T> const T& MAX(const T& a, const T& b)...
7
by: sachin_mzn | last post by:
Hi, It may be a silly question but I want to know the difference between #define macro and inline functions Is there any performance issue related to it. -Sachin
11
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
3
by: ashwani | last post by:
Hi Can any one tell me the difference between preprocessor macros like #define and enum. If i want to define MAX_LIMIT=100 as preprocessor macro as #define MAX_LIMIT 100 or if i define enum...
0
by: Jason | last post by:
What is the difference between 'const' and 'define()' ? When would I prefer using 'const' over 'define', or vice versa? It seems if i do: const secondsInMinute = 60; define("secondsInMinute",...
2
by: david wolf | last post by:
My understanding is that cstdio basically is the same as stdio.h except the functions are in a namspace called std. However when I take a look at the content of the file cstdio, it has the...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
34
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
6
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.