473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Nov 16 '06 #1
9 5900
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.
js*******@attglobal.net
==================
Nov 16 '06 #2
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.
js*******@attglobal.net
==================
Nov 16 '06 #3
..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
Nov 17 '06 #4

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

Nov 17 '06 #5
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.
js*******@attglobal.net
==================
Nov 17 '06 #6
..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
Nov 17 '06 #7
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
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Nov 17 '06 #8
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.
js*******@attglobal.net
==================
Nov 17 '06 #9
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.
js*******@attglobal.net
==================
Nov 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
1
by: Patrick Stinson | last post by:
I am trying to create a way to register static members of a **class**, not an object, for making an object factory. The main thing I want is to be able to make a call like class MyClass {...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
9
by: Jay Douglas | last post by:
Hello, I am needing to pass a class object (this) by reference to a method in a different class. When I do the following code I get the error (Cannot pass '<this>' as a ref or out argument because...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
13
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
1
Markus
by: Markus | last post by:
This is for you pb - you're looking a little bored; answering all those old threads ;) So here's what ive got, it's a validation function: <?php /* *$username = $_POST; *$email = $_POST; *...
1
by: manuitpro | last post by:
I am using class and and connection variable $this->_connection = @fsockopen($this->_hostname, 23, $errno, $errstr, $this->_timeout), class has other variables also. class object is global, and when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.