From: Peter van der Does on
Hi,

How do you people store data that doesn't change, an example of this
would be the version number of your software. You might want to use it
through out your program but how to you store it?

As far as I can see there are several options to use this data.
1. Global Variable
2. Store it in a registry class
3. Store it in a named constant.
4. Use a function that will return the data (kind of like a regsitry
class but it's not a class)

Personally I don't like option 1 but what about the other options. Is
any of them faster then the others. What other pros and cons are there.

--
Peter van der Does

GPG key: E77E8E98

IRC: Ganseki on irc.freenode.net
Twitter: @petervanderdoes

WordPress Plugin Developer
Blog: http://blog.avirtualhome.com
Forums: http://forums.avirtualhome.com
Twitter: @avhsoftware
From: Peter Lind on
On 15 September 2010 14:46, Peter van der Does <pvanderdoes(a)gmail.com> wrote:
> Hi,
>
> How do you people store data that doesn't change, an example of this
> would be the version number of your software. You might want to use it
> through out your program but how to you store it?
>
> As far as I can see there are several options to use this data.
> 1. Global Variable
> 2. Store it in a registry class
> 3. Store it in a named constant.
> 4. Use a function that will return the data (kind of like a regsitry
> class but it's not a class)
>
> Personally I don't like option 1 but what about the other options. Is
> any of them faster then the others. What other pros and cons are there.
>

3. Constant, as long as you're dealing with scalars.

Regards
Peter

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
BeWelcome/Couchsurfing: Fake51
Twitter: http://twitter.com/kafe15
</hype>
From: Ashley Sheridan on
On Wed, 2010-09-15 at 08:46 -0400, Peter van der Does wrote:

> Hi,
>
> How do you people store data that doesn't change, an example of this
> would be the version number of your software. You might want to use it
> through out your program but how to you store it?
>
> As far as I can see there are several options to use this data.
> 1. Global Variable
> 2. Store it in a registry class
> 3. Store it in a named constant.
> 4. Use a function that will return the data (kind of like a regsitry
> class but it's not a class)
>
> Personally I don't like option 1 but what about the other options. Is
> any of them faster then the others. What other pros and cons are there.
>
> --
> Peter van der Does
>
> GPG key: E77E8E98
>
> IRC: Ganseki on irc.freenode.net
> Twitter: @petervanderdoes
>
> WordPress Plugin Developer
> Blog: http://blog.avirtualhome.com
> Forums: http://forums.avirtualhome.com
> Twitter: @avhsoftware
>


I'd go with a constant, for the simple reason that it means the variable
can't and shouldn't change in the execution of the script. The constant
could be within a class or a global, however you prefer. There will be
slightly more memory used in a class, but it avoids the constant from
conflicting with others in the script possibly.

Creating a whole function or class for this does seem a little overkill
probably, so a simple global constant should do the job really.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: tedd on
At 8:46 AM -0400 9/15/10, Peter van der Does wrote:
>Hi,
>
>How do you people store data that doesn't change, an example of this
>would be the version number of your software. You might want to use it
>through out your program but how to you store it?
>
>As far as I can see there are several options to use this data.
>1. Global Variable
>2. Store it in a registry class
>3. Store it in a named constant.
>4. Use a function that will return the data (kind of like a regsitry
>class but it's not a class)
>
>Personally I don't like option 1 but what about the other options. Is
>any of them faster then the others. What other pros and cons are there.

Make it's a Constant -- it's simply a Global that doesn't change.

I typically hold such things in a global configuration file that can
be included when needed.

Cheers,

tedd

--
-------
http://sperling.com/
From: chris h on
When you really NEED a global resource I'm a fan of a registry class. It's
a little slower but not noticeable in most cases.

With a registry you can store all global data in a single, controlled area.
You can also store arrays and resources instead of just strings. One down
side to most registry classes is that it's not truly "constant" data, but
with magic methods you can correct that.


In this class you can store values globally, and once they are set they
can't be easily changed.

myRegistry::get()->test = array('abc','xyz');
myRegistry::get()->test = 1234;

echo myRegistry::get()->test[1]; // should echo 'xyz'


I just typed this up real quick so there may be errors; but if you want to
try it, it should at least give you the just of it.
-------------untested code----------------
class myRegistry {

private $_Data = array();

static private $_Object = null;


static public function get () {

if( !( self::$_Object instanceof self ))
self::$_Object = new self();

return self::$_Object;

}


private function __construct () { }


public function __get ($name) {

if (isset( $this->_Data[$name] ))
return $this->_Data[$name];

return null;

}


public function __set ($name, $value) {

if (isset( $this->_Data[$name] ))
return false;

return $this->_Data[$name] = $value;

}

}
------------------

On Thu, Sep 16, 2010 at 10:50 AM, tedd <tedd.sperling(a)gmail.com> wrote:

> At 8:46 AM -0400 9/15/10, Peter van der Does wrote:
>
>> Hi,
>>
>> How do you people store data that doesn't change, an example of this
>> would be the version number of your software. You might want to use it
>> through out your program but how to you store it?
>>
>> As far as I can see there are several options to use this data.
>> 1. Global Variable
>> 2. Store it in a registry class
>> 3. Store it in a named constant.
>> 4. Use a function that will return the data (kind of like a regsitry
>> class but it's not a class)
>>
>> Personally I don't like option 1 but what about the other options. Is
>> any of them faster then the others. What other pros and cons are there.
>>
>
> Make it's a Constant -- it's simply a Global that doesn't change.
>
> I typically hold such things in a global configuration file that can be
> included when needed.
>
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com/
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
 |  Next  |  Last
Pages: 1 2
Prev: Xpath arguments in variable
Next: Install library