Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > 'Define vs const' or 'file vs class'
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 1st November 23:25
jsf
External User
 
Posts: 1
Default 'Define vs const' or 'file vs class'



Hi everybody

This post is not so much a question to solve a problem but more in the
direction: what would you do and why.

I'm starting a new project and is preparing the basic layout for the
application. In all my previous projects I have had a config file that
all other files would require where I used 'define' to specify database
connection parameters, site root, picture root and stuff like that.

This works without problems, but as I have started to code more in a OO
way, I was wondering if it would not me bore clean to create a site
class with constants for all these values so instead of doing:

echo '<a href=”' . ROOT . '”>Go home</a>';

I would do this:

echo '<a href=”' . Site::ROOT . '”>Go home</a>';

The second might be more verbose in this case, but for a lot of values,
being able to associate them with the site could prove quite valuable if
you or someone else has to look at the code half a year from when it was


The verbose issue aside, having a config file separate from the actual
code seems intuitively more clean (in my mind at least) and using
a class for storing config values might no be the best of ideas. But the
site class could be build from the config file either each time
a script was requested or as part of the build process when your
application is deployed to the server.

Either way... both solutions would work.... what I'm looking for here is
maybe some comments to the ideas before I go ahead with one of them.

Joe
  Reply With Quote


 


2 1st November 23:25
quickshiftin
External User
 
Posts: 1
Default 'Define vs const' or 'file vs class'



in this case there is really no difference, especially if Site contains all
the values
that were originally in the file with define directives, the structure is
essentially the same.

if you are going to have just one class contain all of the configuration
values
there wont be much difference from using define directives. one thing
about
define is its notoriously slow, so you would have that advantage.
generally, a benefit of using classes w/ constants is the namespace aspect.
so you could have Car:EFAULT_COLOR and Plane:EFAULT_COLOR
for example, but again, how much different is that from
define('DEFAULT_CAR_COLOR', 'red');
define('DEFAULT_PLANE_COLOR', 'blue');
i dunno. to be honest i typically use a mixture of both approaches. class
constants
for classes when they are appropriate and define directives for global
configuration
values.
strictly speaking i dont think having a class of all constants qualifies an
app
as 'more oo'; id say in java for example you simply dont have any other
choice.

-nathan
  Reply With Quote
Reply


Thread Tools
Display Modes




666