473,549 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("seconds InMinute", 60);

Aren't these two the same thing?

- Jason

Oct 12 '06 #1
2 7981
"Jason" <ja********@gma il.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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("seconds InMinute", 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***@bhg byrzcv.arg)
Oct 12 '06 #2
It's all about the scope.
class BlahBlah
{
const MY_CONSTANT = 45676;
}
define('MY_CONS TANT', 76344);
echo BlahBlah::MY_CO NSTANT . "\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("seconds InMinute", 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
5719
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) { return (a > b ? a : b); }
7
1956
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
2496
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 the best match. Anyone could give a detail explanation in theory? Which one is good?
3
11654
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 {MAX_LIMIT=100}; then what is the basic difference? Is there any efficiency tradeoff between both the definitions.
0
410
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", 60); Aren't these two the same thing? - Jason
2
14554
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 following lines inside only: -------content of cstdio on red hat linux enterprise 3---- #ifndef __CSTDIO__ #define __CSTDIO__ #include <stdio.h> #endif
6
4015
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 types of objects (classes) can only be defined in Class modules.
34
2658
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
6
2250
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 access label are implicitly private; ifwe usethe struct keyword, then those members are public. Whether we define a class using the class keyword or the...
0
7541
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5385
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5107
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3512
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1960
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.