Re: 'Define vs const' or 'file vs class'

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Dec 17, 2007 8:15 AM, Johannes Skov Frandsen <jsf@xxxxxxxxxxx> 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux