|
Posted by Jerry Stuckle on 11/17/06 02:26
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@attglobal.net
==================
Navigation:
[Reply to this message]
|