By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,277 Members | 2,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,277 IT Pros & Developers. It's quick & easy.

One must use globals in ones classes

P: n/a
I get the impression that most people who are using objects in their
PHP projects are mixing them with procedural code. The design, I
think, is one where the procedural code is the client code, and the
classes are a library of utility code.

As such, when I've asked about how to get globals into objects, I've
been told that I should pass them in as the parameters to a method.
Easy enough if you have some procedural code. This answer I've been
given assumes that there is something outside of the object, some
procedural code that can get hold of the global and then hand it to
the object.

However, am I wrong in saying that if you wanted to go pure OO, you'd
have to grab the globals from inside of the objects? You could limit
this to the constructor, and possibly you could limit to just a single
master object that contained all other objects, but still, you'd have
to do pull globals straight into the object, yes?
Jul 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
lawrence wrote:
However, am I wrong in saying that if you wanted to go pure OO, you'd
have to grab the globals from inside of the objects? You could limit
this to the constructor, and possibly you could limit to just a single
master object that contained all other objects, but still, you'd have
to do pull globals straight into the object, yes?


Basically, you want to limit the use of globals to bare-minimums, globals
are bad mmkay, just one small step from globals to goto :-)

If you absolutely must use globals, best way would be to use
$GLOBAL['varname'] that way there is no mistaking the fact that you are
accessing something in the global scope.

--
James Sleeman
Gogo:Code, http://www.gogo.co.nz/
PHP & Coldfusion Programming Services
Email domain : gogo.co.nz see user in from header!
Jul 16 '05 #2

P: n/a
Hi Lawrence!
On 14 Jul 2003 13:20:24 -0700, lk******@geocities.com (lawrence)
wrote:
I get the impression that most people who are using objects in their
PHP projects are mixing them with procedural code. The design, I
think, is one where the procedural code is the client code, and the
classes are a library of utility code.

As such, when I've asked about how to get globals into objects, I've
been told that I should pass them in as the parameters to a method.
Easy enough if you have some procedural code. This answer I've been
given assumes that there is something outside of the object, some
procedural code that can get hold of the global and then hand it to
the object.

However, am I wrong in saying that if you wanted to go pure OO, you'd
have to grab the globals from inside of the objects? You could limit
this to the constructor, and possibly you could limit to just a single
master object that contained all other objects, but still, you'd have
to do pull globals straight into the object, yes?


Yes and no.

If you pass the variables as parameters, you control the scope of the
variables well, but the interfaces to your class get bloated. This
makes it hard to change them and hard to test the functions.

If you use globals, you have no control of the scope, ie. you may
overwrite a different variable in another function, because it is
global.

A solution I use is a mediator object, which is
- a proper OO solution (see the Patterns book by Gamma)
- centralizes the control of the global variables into one class. I
use this to create only one object, which allows me to share the same
information and not create any unnecessary objects

A mediator object goes like this:

class mediator{

function &object_instance(){

static $instance;
if (!isset($instance)){
$instance = new object();
}

return $instance;
}
}
You can put more functions into the object, if you want.

Instead of

global $object;

in a function you now use

$object =& mediator::object_instance();
HTH,

jochen

--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 16 '05 #3

P: n/a
I'm finding this thread useful and interesting because it's touching on a
question I had. I'm not a programmer (except in a very amateur sense) and
have never used objects before. But I've just started.

For one application I have an object representing a project (name, contact
name etc etc). The data for the projects is held in a MySQL database. There
is a method that I've written that requires access to a variable and a
function outside the object, and I had a gut feeling this might be bad
programming.

This method writes the object's properties to the database, and having this
method in the object tidies up and simplifies the main code greatly.
However, the method needs access to the db link variable which I've
established outside the object with mysql_connect (this routine is in an
include file that is kept outside of the public_html path). I don't want to
repeat this routine inside the object as it requires the username, password
etc. Also, I have a function (also in an include file - a standard one that
is included on every page) that handles database errors, and so the object
needs to be able to call this function. That means the object becomes
dependent on these include files - is that bad? And if so, is there an
elegant way around this? I guess I could pull out this whole method and put
it in an include file as a function, but that's a little less tidy.
Jul 16 '05 #4

P: n/a
Quinn wrote:
I'm finding this thread useful and interesting because it's touching on a
question I had. I'm not a programmer (except in a very amateur sense) and
have never used objects before. But I've just started.

For one application I have an object representing a project (name, contact
name etc etc). The data for the projects is held in a MySQL database. There
is a method that I've written that requires access to a variable and a
function outside the object, and I had a gut feeling this might be bad
programming.
This method writes the object's properties to the database, and having this
method in the object tidies up and simplifies the main code greatly.
However, the method needs access to the db link variable which I've
established outside the object with mysql_connect (this routine is in an
include file that is kept outside of the public_html path). I don't want to
repeat this routine inside the object as it requires the username, password
etc. Also, I have a function (also in an include file - a standard one that
is included on every page) that handles database errors, and so the object
needs to be able to call this function. That means the object becomes
dependent on these include files - is that bad? And if so, is there an
elegant way around this? I guess I could pull out this whole method and put
it in an include file as a function, but that's a little less tidy.


Well... An object is behavior + state.

Functions which are really functions (ie : don't have any side effect)
are stateless, they only relie on their input (arguments) and do not
modify any state (static or global variable). I don't see the point of
having these functions as methods of a class (at least in PHP - in some
110% OOPL's, you may not have the choice !-), so keeping them apart from
the object model is quite ok - at least I do it whithout any guilty
feeling !-)

Yes, it makes the objects using these functions dependent of the
includes. No big deal, you'll always have dome dependency somewhere.
Now for your problem, you have an object that depend on a database
connection, and on some database error handling functions. I don't have
much context here, but I can think of basically two ways to manage this
cleanly :

1/ There aren't any relationships at all between the db connection and
the db error handlers : make the db connection a property of the
'project' object (you pass the db connection as an argument to the
constructor), and keep the functions apart.

2/ There is some 'state' relationship between the db connection and the
error handlers : wrap all this in a class, and pass an instance of that
class to the constructor of the 'project' object. An object can be an
attribute of another object.

Now there is nothing as a one-size-fits-all answer...

HTH
Bruno

Jul 16 '05 #5

P: n/a
lawrence wrote:
I get the impression that most people who are using objects in their
PHP projects are mixing them with procedural code. The design, I
think, is one where the procedural code is the client code, and the
classes are a library of utility code.
Right.
As such, when I've asked about how to get globals into objects, I've
been told that I should pass them in as the parameters to a method.
Easy enough if you have some procedural code. This answer I've been
given assumes that there is something outside of the object, some
procedural code that can get hold of the global and then hand it to
the object.
Right also.
However, am I wrong in saying that if you wanted to go pure OO,
Just a point : in PHP, you will at least need 2 lines of procedural
code. Guess why ?-) (answer below)

you'd
have to grab the globals from inside of the objects? You could limit
this to the constructor, and possibly you could limit to just a single
master object that contained all other objects,
Not necessarily 'contain', nor 'all other objects'... It can instanciate
some objects that will instanciate others, pass objects to other objects
etc...
but still, you'd have
to do pull globals straight into the object, yes?


Right. When you have something 'ugly' to do, do it in just one point.
BTW, the singleton pattern (cf Jochen's post in this thread) might be
useful for this 'master' object.

-- 2 lines of procedural code --
<?php
myMasterObject = new MyMasterClass();
myMasterObject->run();
?>

How would your classes etc live without at least these 2 lines ?

HTH
Bruno

Jul 16 '05 #6

P: n/a
With total disregard for any kind of safety measures Bruno
Desthuilliers <bd***********@removeme.free.fr> leapt forth and
uttered:
How would your classes etc live without at least these 2 lines ?

HTH
Bruno


Mine only need one line:

new MyObject;

It would be nice if PHP had an auto-run method similar to main() in
Java/C#/C++/etc though.

--
There is no signature.....
Jul 16 '05 #7

P: n/a
Phil Roberts wrote:
With total disregard for any kind of safety measures Bruno
Desthuilliers <bd***********@removeme.free.fr> leapt forth and
uttered:

How would your classes etc live without at least these 2 lines ?

HTH
Bruno

Mine only need one line:

new MyObject;


Got me !-)

Having this kind of side-effects in a constructor is very ugly, but yes,
it works.

Bruno
Jul 16 '05 #8

P: n/a
On Tue, 15 Jul 2003 12:33:32 +0000, Bruno Desthuilliers wrote:
Functions which are really functions (ie : don't have any side effect)
are stateless, they only relie on their input (arguments) and do not
modify any state (static or global variable). I don't see the point of
having these functions as methods of a class (at least in PHP - in some
110% OOPL's, you may not have the choice !-), so keeping them apart from
the object model is quite ok - at least I do it whithout any guilty
feeling !-) Yes, it makes the objects using these functions dependent of the
includes. No big deal, you'll always have dome dependency somewhere.
Now for your problem, you have an object that depend on a database
connection, and on some database error handling functions. I don't have
much context here, but I can think of basically two ways to manage this
cleanly : 1/ There aren't any relationships at all between the db connection and
the db error handlers : make the db connection a property of the
'project' object (you pass the db connection as an argument to the
constructor), and keep the functions apart. 2/ There is some 'state' relationship between the db connection and the
error handlers : wrap all this in a class, and pass an instance of that
class to the constructor of the 'project' object. An object can be an
attribute of another object. Now there is nothing as a one-size-fits-all answer... HTH
Bruno


Thanks for the advice. I guess I could always pass that link into the
object as a parameter to the method (I already pass one, telling the mthod
whether to write a new record or update an existing one, though I think
I've worked out a way to do that automatically).
Jul 16 '05 #9

P: n/a
Bruno Desthuilliers <bd***********@removeme.free.fr> wrote in message
However, am I wrong in saying that if you wanted to go pure OO,


Just a point : in PHP, you will at least need 2 lines of procedural
code. Guess why ?-) (answer below)


Right. I guess I was assuming that everyone could figure out that much
on their own, but I should have been more clear. On the last 10
websites I've done, the index page has consisted of these 4 lines:
<?php
include("mcIncludes/mcConfig.php");
runMainLoop();
?>
Jul 16 '05 #10

P: n/a
Jochen Daum <jo*********@cans.co.nz> wrote in message
A solution I use is a mediator object, which is
- a proper OO solution (see the Patterns book by Gamma)
- centralizes the control of the global variables into one class. I
use this to create only one object, which allows me to share the same
information and not create any unnecessary objects

A mediator object goes like this:

class mediator{

function &object_instance(){

static $instance;
if (!isset($instance)){
$instance = new object();
}

return $instance;
}
}


Without a doubt, this seems the correct way to go.
Jul 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.