'Define vs const' or 'file vs class'

This is a discussion on 'Define vs const' or 'file vs class' within the PHP General forums, part of the PHP Programming Forums category; Hi everybody This post is not so much a question to solve a problem but more in the direction: what ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-17-2007
Johannes Skov Frandsen
 
Posts: n/a
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
original written.

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 (permalink)  
Old 12-17-2007
Nathan Nobbe
 
Posts: n/a
Default Re: [PHP] 'Define vs const' or 'file vs class'

On Dec 17, 2007 8:15 AM, Johannes Skov Frandsen <jsf@greenoak.dk> wrote:

> 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
> original written.



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.


> 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.



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::DEFAULT_COLOR and Plane::DEFAULT_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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:59 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0