making a class object global

Hi gurus

I have a class from which I create an object. I want this object to be
available in all my functions for my application. Basically, I want to
make the object global. What is the best way to do this?

Thank you

Nicolaas
WindAndWaves [ Fr, 17 November 2006 00:47 ] [ ID #1538384 ]

Re: making a class object global

windandwaves wrote:
> Hi gurus
>
> I have a class from which I create an object. I want this object to be
> available in all my functions for my application. Basically, I want to
> make the object global. What is the best way to do this?
>
> Thank you
>
> Nicolaas
>

Like any other variable. Pass it as a parameter to the functions
(highly recommended) or make it global (not recommended).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Fr, 17 November 2006 00:51 ] [ ID #1538385 ]

Re: making a class object global

Thanks Jerry

I know about those methods. That is cool, problem is that I dont want
to add an extra variable to each function (over 30) that I have for
this site.

Anyway, I will stick with that for now.

Thanks a million for your reply.

Nicolaas


Jerry Stuckle wrote:
> windandwaves wrote:
> > Hi gurus
> >
> > I have a class from which I create an object. I want this object to be
> > available in all my functions for my application. Basically, I want to
> > make the object global. What is the best way to do this?
> >
> > Thank you
> >
> > Nicolaas
> >
>
> Like any other variable. Pass it as a parameter to the functions
> (highly recommended) or make it global (not recommended).
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstucklex [at] attglobal.net
> ==================
WindAndWaves [ Fr, 17 November 2006 01:08 ] [ ID #1539655 ]

Re: making a class object global

..oO(windandwaves)

>I have a class from which I create an object. I want this object to be
>available in all my functions for my application. Basically, I want to
>make the object global. What is the best way to do this?

You could search for "singleton".

Micha
Michael Fesser [ Fr, 17 November 2006 01:32 ] [ ID #1539658 ]

Re: making a class object global

Michael Fesser wrote:
> .oO(windandwaves)
>
> >I have a class from which I create an object. I want this object to be
> >available in all my functions for my application. Basically, I want to
> >make the object global. What is the best way to do this?
>
> You could search for "singleton".
>
> Micha

Thanks Micha

That is really cool. I was just reading about static and this is a
great way to use it. Thanks a million for the recommendation. Cool
stuff. What would actually be ideal is if there were a function like
global that you could use when creating an object, which would make the
object global without having to do this singleton stuff. It would
actually make sense if the global $myobj just did this if it were used
outside of a function.

Thanks

Nicolaas
WindAndWaves [ Fr, 17 November 2006 03:15 ] [ ID #1539666 ]

Re: making a class object global

windandwaves wrote:
> Jerry Stuckle wrote:
>
>>windandwaves wrote:
>>
>>>Hi gurus
>>>
>>>I have a class from which I create an object. I want this object to be
>>>available in all my functions for my application. Basically, I want to
>>>make the object global. What is the best way to do this?
>>>
>>>Thank you
>>>
>>>Nicolaas
>>>
>>
>>Like any other variable. Pass it as a parameter to the functions
>>(highly recommended) or make it global (not recommended).
>>
> Thanks Jerry
>
> I know about those methods. That is cool, problem is that I dont want
> to add an extra variable to each function (over 30) that I have for
> this site.
>
> Anyway, I will stick with that for now.
>
> Thanks a million for your reply.
>
> Nicolaas
>
>

(Top posting fixed)

Ok, there's another way have a static method to return it, i.e.
(PHP5 - and not tested so may contain some syntax errors)

class Test {
private static $me = null;

public static function getTest() {
if (Test::$me == null)
Test::$me = new Test;
return Test::$me;
}
}

Test::$me is a private static variable which is shared amongst all
instances. The function getTest checks Test::$me to see if it is set.
If not, getTest() allocates a new Test object and assigns it to
Test::$me. It then returns the value.

To call it, you just use:

$test = Test::$getTest();

P.S. Please don't top post. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Fr, 17 November 2006 03:26 ] [ ID #1539667 ]

Re: making a class object global

..oO(Jerry Stuckle)

>Ok, there's another way have a static method to return it, i.e.
> (PHP5 - and not tested so may contain some syntax errors)
>
>class Test {
> private static $me = null;
>
> public static function getTest() {
> if (Test::$me == null)
> Test::$me = new Test;
> return Test::$me;
> }
>}

Should work, even if I would call it getInstance() to be more generic.
You could also use the keyword 'self' instead of the class name inside
the function:

public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}

Then if you should need this function in another class you just have to
copy it. But that's more or less just personal preference. It works the
same.

>Test::$me is a private static variable which is shared amongst all
>instances.

JFTR: There's always only _one_ instance. That's why this is called a
singleton.

Micha
Michael Fesser [ Fr, 17 November 2006 11:32 ] [ ID #1539694 ]

Re: making a class object global

Following on from Jerry Stuckle's message. . .
>windandwaves wrote:
>> Hi gurus
>>
>> I have a class from which I create an object. I want this object to be
>> available in all my functions for my application. Basically, I want to
>> make the object global. What is the best way to do this?
>>
>> Thank you
>>
>> Nicolaas
>>
>
>Like any other variable. Pass it as a parameter to the functions
>(highly recommended) or make it global (not recommended).
>

Or stick it in $_SESSION. If you do that then make sure you understand
the PHP version of variable/pointers/references and the use of &.




--
PETER FOX Not the same since the bookshop idea was shelved
peterfox [at] eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Peter Fox [ Fr, 17 November 2006 11:17 ] [ ID #1539695 ]

Re: making a class object global

Michael Fesser wrote:
> .oO(Jerry Stuckle)
>
>
>>Ok, there's another way have a static method to return it, i.e.
>> (PHP5 - and not tested so may contain some syntax errors)
>>
>>class Test {
>> private static $me = null;
>>
>> public static function getTest() {
>> if (Test::$me == null)
>> Test::$me = new Test;
>> return Test::$me;
>> }
>>}
>
>
> Should work, even if I would call it getInstance() to be more generic.
> You could also use the keyword 'self' instead of the class name inside
> the function:
>
> public static function getInstance() {
> if (!isset(self::$instance)) {
> self::$instance = new self();
> }
> return self::$instance;
> }
>
> Then if you should need this function in another class you just have to
> copy it. But that's more or less just personal preference. It works the
> same.
>
>
>>Test::$me is a private static variable which is shared amongst all
>>instances.
>
>
> JFTR: There's always only _one_ instance. That's why this is called a
> singleton.
>
> Micha

Micha,

Yes, this works great. I've been using similar code in C++ since the 80's.

Using the class name or self is a matter of programming style. I prefer
the class name - I find it to be easier for new programmers to understand.

And that's the same reason I don't use buzzwords such as "singleton"
when describing how code works. I prefer a clear explanation in simple
English, especially here where for many readers English isn't a first
language.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Fr, 17 November 2006 13:30 ] [ ID #1539704 ]

Re: making a class object global

Peter Fox wrote:
> Following on from Jerry Stuckle's message. . .
>
>> windandwaves wrote:
>>
>>> Hi gurus
>>>
>>> I have a class from which I create an object. I want this object to be
>>> available in all my functions for my application. Basically, I want to
>>> make the object global. What is the best way to do this?
>>>
>>> Thank you
>>>
>>> Nicolaas
>>>
>>
>> Like any other variable. Pass it as a parameter to the functions
>> (highly recommended) or make it global (not recommended).
>>
>
> Or stick it in $_SESSION. If you do that then make sure you understand
> the PHP version of variable/pointers/references and the use of &.
>
>
>
>

How is that going to help? It's a complete misuse of the $_SESSION
variable - which was never meant to replace the other superglobals such
as $_GLOBAL.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Fr, 17 November 2006 13:32 ] [ ID #1539705 ]
PHP » comp.lang.php » making a class object global

Vorheriges Thema: How to "fetch" a certain id
Nächstes Thema: Use parameters within a Function, please help me