Using globals or references? 
Author Message
 Using globals or references?

I was wondering what is most commonly used, globals or references (and
what is the best method for PHP):

For instance, I have two classes, one which is the database connection
class and another one which is a data class.

I see two options:

1st option:
***********
main page:
----------
global $database();
$database = new Database();
$data = new $Data();

data class:
-----------
class Data {

function Data() {
$this->populate();

Quote:
}

function populate() {
global $database;
$database->query(...);

Quote:
}
}

2nd option:
***********
main page:
----------
$database = new Database();
$data = new $Data($database);

data class:
-----------
class Data {

function Data(&$database) {
$this->database = &$database;
$this->populate();

Quote:
}

function populate() {
$this->database->query(...);

Quote:
}
}

Any comments?

Best regards,
cj.



Mon, 25 Apr 2005 23:29:44 GMT  
 Using globals or references?

Quote:

> I was wondering what is most commonly used, globals or references (and
> what is the best method for PHP):

Most people code PHP as a simple scripting language and do everything in
global space. The larger an app gets, the more maintenance problems arise
from that approach.

Quote:
> For instance, I have two classes, one which is the database connection
> class and another one which is a data class.

IMO, use references when working with classes (when possible/feasible). One
major goal of classes is to keep stuff out of global space. It'll help make
your objects more portable across PHP app frameworks, as well, meaning
other people can potentially use your code without having to change so much
of it.

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Tue, 26 Apr 2005 00:42:40 GMT  
 Using globals or references?

Quote:


>>I was wondering what is most commonly used, globals or references (and
>>what is the best method for PHP):

> Most people code PHP as a simple scripting language and do everything in
> global space. The larger an app gets, the more maintenance problems arise
> from that approach.

>>For instance, I have two classes, one which is the database connection
>>class and another one which is a data class.

> IMO, use references when working with classes (when possible/feasible). One
> major goal of classes is to keep stuff out of global space. It'll help make
> your objects more portable across PHP app frameworks, as well, meaning
> other people can potentially use your code without having to change so much
> of it.

Hi Stephan,

I have to agree with you, I did some more reading and found out that it
is quite easy to use singletons in PHP.  This is great news for me since
   now I can make my database access class, language class, etc. a
singleton, which eliminates many possible problems.

I'm beginning to like PHP more and more, I still miss some Java OO
features, but I'm beginning to realize that you can do serious OO
programming in PHP too without major drawbacks, something I didn't
believe was possible when I started with PHP a few weeks ago.

Best regards,
cj.



Tue, 26 Apr 2005 01:25:30 GMT  
 Using globals or references?

Quote:

> I'm beginning to like PHP more and more, I still miss some Java OO
> features, but I'm beginning to realize that you can do serious OO
> programming in PHP too without major drawbacks, something I didn't
> believe was possible when I started with PHP a few weeks ago.

The biggest drawback, IMO, is that working with references in PHP is a pain
in the {*filter*}(because you have to be sure to use the "get a reference"
syntax). Also, i have seen several cases where objects simply disappear at
the end of the function if i create them as refrences but work fine when i
create them as copies.

IMO it should do OO Java-style: references for everything except primitive
types.

:)

--
----- stephan beal
Registered Linux User #71917 http://www.*-*-*.com/
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Tue, 26 Apr 2005 01:30:09 GMT  
 Using globals or references?

Quote:

> IMO it should do OO Java-style: references for everything except primitive
> types.

Do you say that because you miss writing stuff like:

new Integer((((Integer) genres.get(current.toString())).intValue() + 1)));

In this particular case, this is equivalent to what you in PHP would write
as:

count = $genres[current]++;

;)

Andr N?ss



Tue, 26 Apr 2005 02:41:00 GMT  
 Using globals or references?

Quote:

> Do you say that because you miss writing stuff like:

> new Integer((((Integer) genres.get(current.toString())).intValue() + 1)));

That makes a copy, which is what i don't generally have a need to do
(references are what i want in 98% of cases). With PHP i'm tired of having
to use the reference syntax, mainly because it works with or without the
syntax until you notice that you manipulated a copy and have to go back and
put the & after the = sign.

Quote:
> count = $genres[current]++;

------------------^^^^^^^ always quote your keys ;)

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Tue, 26 Apr 2005 02:45:55 GMT  
 Using globals or references?

Quote:


>> Do you say that because you miss writing stuff like:

>> new Integer((((Integer) genres.get(current.toString())).intValue() +
>> 1)));

> That makes a copy,

Okay, not an exact copy, perhaps, but a new object.

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Tue, 26 Apr 2005 02:46:27 GMT  
 Using globals or references?

Quote:


>> Do you say that because you miss writing stuff like:

>> new Integer((((Integer) genres.get(current.toString())).intValue() +
>> 1)));

> That makes a copy, which is what i don't generally have a need to do
> (references are what i want in 98% of cases). With PHP i'm tired of having
> to use the reference syntax, mainly because it works with or without the
> syntax until you notice that you manipulated a copy and have to go back
> and put the & after the = sign.

That wasn't my point, my point was the absolutely horrendous amount of
totally obscure code needed to do something as simple as incrementing some
entry in a dictionary/associative array/hashtable by one. This because Java
doesn't allow you to store a basic type in it's Collections, which, if you
ask me, is so braindead it's reason enough to stay far far away from Java
:)

Andr N?ss



Tue, 26 Apr 2005 03:23:53 GMT  
 Using globals or references?

Quote:

> > I'm beginning to like PHP more and more, I still miss some Java OO
> > features, but I'm beginning to realize that you can do serious OO
> > programming in PHP too without major drawbacks, something I didn't
> > believe was possible when I started with PHP a few weeks ago.

> The biggest drawback, IMO, is that working with references in PHP is a
pain
> in the {*filter*}(because you have to be sure to use the "get a reference"
> syntax). Also, i have seen several cases where objects simply disappear at
> the end of the function if i create them as refrences but work fine when i
> create them as copies.

> IMO it should do OO Java-style: references for everything except primitive
> types.

I disagree - but that is maybe just because I am used to working in
languages that deal with copies and not references.

I had a colleauge who was bit in the {*filter*}by Flash ActionScript ... it
defaulted to passing arrays by reference (without any documentation) and
caused hours of debugging.

Check this article on php Reference counting:
http://www.*-*-*.com/

It may be a split hair, but it gives many of the benefits of referencing,
while still allowing a default copy behaviour.  Php will not create a new
value in memory unless you change the value (i.e. the variable is rebound
only when changing the value).  This means there is little benefit to
passing by reference unless you want to change the original datastructure
(which in most cases I do not want to do).  If you are simply iterating
through an array or traversing the nodes of an object, php defaults to
passing by reference.

regards,
reggie.



Tue, 26 Apr 2005 06:14:52 GMT  
 Using globals or references?

Quote:
> I'm beginning to like PHP more and more, I still miss some Java OO
> features, but I'm beginning to realize that you can do serious OO
> programming in PHP too without major drawbacks, something I didn't
> believe was possible when I started with PHP a few weeks ago.

Dont' get too into OOP in PHP.  There are drawbacks.

http://www.zend.com/zend/art/mistake1.php#Heading13

excerpt from the article:

    Note: Generally speaking, an application that uses OO support
    will be slower just as using eval() will be slower than normal
    code. To amply show places where OO support gets somewhat ugly,
    I'd have to use advanced PHP features and concepts, some of
    which have not even been documented yet.

The best use for classes in PHP (IMO) is to abstract and to preserve
namespace.  Actually adopting a OOP style may prove both difficult, and
counter productive.

regards,
reggie



Tue, 26 Apr 2005 06:20:46 GMT  
 Using globals or references?
Quote:



>>I'm beginning to like PHP more and more, I still miss some Java OO
>>features, but I'm beginning to realize that you can do serious OO
>>programming in PHP too without major drawbacks, something I didn't
>>believe was possible when I started with PHP a few weeks ago.

> Dont' get too into OOP in PHP.  There are drawbacks.

> http://www.zend.com/zend/art/mistake1.php#Heading13

> excerpt from the article:

>     Note: Generally speaking, an application that uses OO support
>     will be slower just as using eval() will be slower than normal
>     code. To amply show places where OO support gets somewhat ugly,
>     I'd have to use advanced PHP features and concepts, some of
>     which have not even been documented yet.

> The best use for classes in PHP (IMO) is to abstract and to preserve
> namespace.  Actually adopting a OOP style may prove both difficult, and
> counter productive.

> regards,
> reggie

I have read that article and that actually was the main reason we I
thought of PHP as not suited for serious OO programming when I first
started with it.  However, this newsgroup has showed me that there are
quite a number of people who use PHP the OO way and really, for large
projects it's so much handier.  Personally, I haven't found any proof
that PHP is 'significantly' slower when using OO (I don't mind if the
code runs a few ms slower if it gets much more manageable).

Anyway, I guess there will always be three camps, 1 that only uses
linear programming, 1 that does everything the OO way and one camp in
the middle.

Best regards,
cj.



Tue, 26 Apr 2005 20:55:08 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Using Globals to pass Control References

2. Globals used in ParcPlace VisualWorks 2.5

3. adding a reference view using the composer

4. Help using reference fields and PROP:Label

5. Using What() on a Reference field

6. Pitfalls of using References in Labview6i

7. Using control reference with a Sub-vi over a TCP VI Server connection

8. Using control reference with a Sub-vi over a TCP VI Server connection

9. Using property node by reference in a for loop

10. Error 1003 when using call by reference node

11. using call by reference node to run a vi

12. Positioning string indicator scrollbar using a control reference?

 

 
Powered by phpBB® Forum Software