Pass variable to variable by reference? 
Author Message
 Pass variable to variable by reference?

I was wondering if there is an easy way to pass variables by reference
in php.  I would find it very useful for clean OO programming.  For
instance, I am used to having getters and setters.  If I would have an
object, called anObject and I would do the following:
aVariable = anObject->getVariable()
I would get a copy of the variable stored in the object instead of a
reference to it.  I am aware that I can use '&' in the function
defenition, but that does not apply here since no variables are
arguments for the functions.

Is the only solution anObject->aVariable or is there a clean OO way?

Best regards,
cj.



Fri, 22 Apr 2005 18:49:12 GMT  
 Pass variable to variable by reference?

Quote:

> I was wondering if there is an easy way to pass variables by reference
> in php.  I would find it very useful for clean OO programming.  For
> instance, I am used to having getters and setters.  If I would have an
> object, called anObject and I would do the following:
> aVariable = anObject->getVariable()
> I would get a copy of the variable stored in the object instead of a
> reference to it.  I am aware that I can use '&' in the function
> defenition, but that does not apply here since no variables are
> arguments for the functions.

function & getVariable() { return $this->aVariable; }

$aVariable =& $anObject->getVariable();

i've had some funny behaviour when using references, though: sometimes
(normally when passing refs of refs of refs) the object pointed to by the
reference simply disappears, for reasons i cannot explain. i'm sure there's
an explanation for it, or a problem in my usage of references, but not that
is immediately obvious.

IMO the best way for PHP to handle this is Java-style: references are used
for everything except primitive types.

--
----- 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.



Fri, 22 Apr 2005 20:00:07 GMT  
 Pass variable to variable by reference?
Thanks for the info, I will try this.  Ofcourse I would also prefer the
java way of doing things (having a java background myself).  Basically
passing everything by value by default tends to have you disregard
encapsulation.  If you just access the object's variables directly, you
don't have any problems.  I have also read that passing variables by
reference is slower than by value in PHP.  Well, maybe the best thing
for us to do is to accept that PHP isn't java and not try to program the
cleanest OO that we can think off.  I already lost so much time on this
and I do wonder if it is worth it in the end.

Best regards and thanks a mill,
cj.

Quote:


>>I was wondering if there is an easy way to pass variables by reference
>>in php.  I would find it very useful for clean OO programming.  For
>>instance, I am used to having getters and setters.  If I would have an
>>object, called anObject and I would do the following:
>>aVariable = anObject->getVariable()
>>I would get a copy of the variable stored in the object instead of a
>>reference to it.  I am aware that I can use '&' in the function
>>defenition, but that does not apply here since no variables are
>>arguments for the functions.

> function & getVariable() { return $this->aVariable; }

> $aVariable =& $anObject->getVariable();

> i've had some funny behaviour when using references, though: sometimes
> (normally when passing refs of refs of refs) the object pointed to by the
> reference simply disappears, for reasons i cannot explain. i'm sure there's
> an explanation for it, or a problem in my usage of references, but not that
> is immediately obvious.

> IMO the best way for PHP to handle this is Java-style: references are used
> for everything except primitive types.



Fri, 22 Apr 2005 21:49:31 GMT  
 Pass variable to variable by reference?

Quote:

> I was wondering if there is an easy way to pass variables by reference
> in php.  I would find it very useful for clean OO programming.  For
> instance, I am used to having getters and setters.  If I would have an
> object, called anObject and I would do the following:
> aVariable = anObject->getVariable()
> I would get a copy of the variable stored in the object instead of a
> reference to it.  I am aware that I can use '&' in the function
> defenition, but that does not apply here since no variables are
> arguments for the functions.

> Is the only solution anObject->aVariable or is there a clean OO way?

Eh... Ah "getter" is presumably a method (to use OO terms) which returns a
*value*, not a variable, so I don't quite see what you want here.

The whole getter/setter thing is really one of the ugliest parts of OO if
you ask me, why on earth should I have to go through the chores of defining
absolutely trivial methods that simply do nothing more than return the
value of some attribute. This has been fixed in C#, afaik.

Andr N?ss



Fri, 22 Apr 2005 23:41:32 GMT  
 Pass variable to variable by reference?

Quote:

> Eh... Ah "getter" is presumably a method (to use OO terms) which returns a
> *value*, not a variable, so I don't quite see what you want here.

> The whole getter/setter thing is really one of the ugliest parts of OO if
> you ask me, why on earth should I have to go through the chores of
> defining absolutely trivial methods that simply do nothing more than
> return the value of some attribute. This has been fixed in C#, afaik.

IMO using getters is one of the BEST applications of OO: it allows
subclasses to override the getter with validation code, for example. If you
grab variables directly you're not "coding for change", and coding for
change/maintainability is the primary driving force behind my OO design.
You can't code for maintainability if users of your objects have direct
access to the variables.

--
----- 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.



Fri, 22 Apr 2005 23:48:19 GMT  
 Pass variable to variable by reference?

Quote:


>> Eh... Ah "getter" is presumably a method (to use OO terms) which returns
>> a *value*, not a variable, so I don't quite see what you want here.

>> The whole getter/setter thing is really one of the ugliest parts of OO if
>> you ask me, why on earth should I have to go through the chores of
>> defining absolutely trivial methods that simply do nothing more than
>> return the value of some attribute. This has been fixed in C#, afaik.

> IMO using getters is one of the BEST applications of OO: it allows
> subclasses to override the getter with validation code, for example. If
> you grab variables directly you're not "coding for change", and coding for
> change/maintainability is the primary driving force behind my OO design.
> You can't code for maintainability if users of your objects have direct
> access to the variables.

My point wasn't that accessing attributes directly (that is, exposing their
implementation) is good. My point was that having to write the
"getters/setters" is totally redundant if they are trivial. After all, say
we have this language X, in X you define the following class:

class Person {
  name String;
  dob Date;
  age derived;

  int get_age() {
    return now - dob; // Ok you get the point...
  }    

Quote:
}

Strictly speaking we don't need age as an attribute, because age is implicit
(we know the persons date of birth and the current date), therefore we
define age as an derived attribute computed by get_age() (and it should
obviously be readonly).

If we have an object p which is an instance of Person, we can say:
d.dob = '1978-07-14'
What happens internally though, is this:
d.set_dob('1978-07-14')
But since set_dob() would be a trivial method which simply updates the dob
attribute we won't have to write it. The same goes for name. This way we
only have to define get/set methods whenever the implementation is anything
but trivial. It would also be nice if one could define attributes as
readonly.

Unfortunately the developers of Java didn't realize this. It seems though
like the developers of python have realized this, at least they have
introduced what they call properties. Then again, Python is incredibly cool
:)

Andr N?ss



Sat, 23 Apr 2005 00:34:45 GMT  
 Pass variable to variable by reference?

Quote:

> If we have an object p which is an instance of Person, we can say:
> d.dob = '1978-07-14'
> What happens internally though, is this:
> d.set_dob('1978-07-14')
> But since set_dob() would be a trivial method which simply updates the dob
> attribute we won't have to write it.

i agree except that if we don't use setters then we cannot change our code
later without updating client code as well (or breaking it). For example,
let's say that we're making our app y2k compliant, and now need to adjust
the given 2-digit year: we do that in the setter and nobody knows the
difference.

Quote:
> The same goes for name. This way we
> only have to define get/set methods whenever the implementation is
> anything but trivial. It would also be nice if one could define attributes
> as readonly.

i agree it's annoying, but i think it's essential for maintainable design.

Quote:
> introduced what they call properties. Then again, Python is incredibly
> cool

*shiver* The syntax drives me crazy. But it is a very popular language, so i
know i'm in the minority on that one.

:)

--
----- 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.



Sat, 23 Apr 2005 00:45:33 GMT  
 Pass variable to variable by reference?

Quote:



>>>Eh... Ah "getter" is presumably a method (to use OO terms) which returns
>>>a *value*, not a variable, so I don't quite see what you want here.

>>>The whole getter/setter thing is really one of the ugliest parts of OO if
>>>you ask me, why on earth should I have to go through the chores of
>>>defining absolutely trivial methods that simply do nothing more than
>>>return the value of some attribute. This has been fixed in C#, afaik.

>>IMO using getters is one of the BEST applications of OO: it allows
>>subclasses to override the getter with validation code, for example. If
>>you grab variables directly you're not "coding for change", and coding for
>>change/maintainability is the primary driving force behind my OO design.
>>You can't code for maintainability if users of your objects have direct
>>access to the variables.

> My point wasn't that accessing attributes directly (that is, exposing their
> implementation) is good. My point was that having to write the
> "getters/setters" is totally redundant if they are trivial. After all, say
> we have this language X, in X you define the following class:

> class Person {
>   name String;
>   dob Date;
>   age derived;

>   int get_age() {
>     return now - dob; // Ok you get the point...
>   }    
> }

> Strictly speaking we don't need age as an attribute, because age is implicit
> (we know the persons date of birth and the current date), therefore we
> define age as an derived attribute computed by get_age() (and it should
> obviously be readonly).

> If we have an object p which is an instance of Person, we can say:
> d.dob = '1978-07-14'
> What happens internally though, is this:
> d.set_dob('1978-07-14')
> But since set_dob() would be a trivial method which simply updates the dob
> attribute we won't have to write it. The same goes for name. This way we
> only have to define get/set methods whenever the implementation is anything
> but trivial. It would also be nice if one could define attributes as
> readonly.

> Unfortunately the developers of Java didn't realize this. It seems though
> like the developers of Python have realized this, at least they have
> introduced what they call properties. Then again, Python is incredibly cool
> :)

> Andr N?ss

Can't say I disagree with you here.  Personally I am getting pretty sick
of always having to type a get and set method for each attribute in an
object.  I realise that there are tools who can make the process a bit
easier, but I do not like auto coding software.  Moreover, all those
getters and setters make the code less readable than without them.  I
would like it very much that if you do not specify a get or set method
for an attribute, a default get and set would be assigned which you
don't explicitely have to code.

Best regards,
cj.



Sat, 23 Apr 2005 00:55:42 GMT  
 Pass variable to variable by reference?

Quote:

> >> The whole getter/setter thing is really one of the ugliest parts of OO
if
> >> you ask me, why on earth should I have to go through the chores of
> >> defining absolutely trivial methods that simply do nothing more than
> >> return the value of some attribute. This has been fixed in C#, afaik.

[OT] any one use C# for a while?  How does it compare (to PHP, Perl or
Python).

I'm all for open source, but I am also more interested in learning good
languages.  Is C# worth it's salt?

feel free to email me if you wish not to jam traffic into this thread.

[/OT]

Quote:
> If we have an object p which is an instance of Person, we can say:
> d.dob = '1978-07-14'
> What happens internally though, is this:
> d.set_dob('1978-07-14')

A common naming system for 'setters' and 'getters' would be all that is
required.

d.dob = '1978-07-14';

1. check to see if a method in the class named set_dob() exists
    if yes, call it
2. check to see if a member variable exists named dob
    if yes, set it's value
3. otherwise create a member variable named dob
    set it's value

Quote:
> But since set_dob() would be a trivial method which simply updates the dob
> attribute we won't have to write it. The same goes for name. This way we
> only have to define get/set methods whenever the implementation is
anything
> but trivial. It would also be nice if one could define attributes as
> readonly.

Before read-only attributes, completely private attributes would be nice.

However, if the above 'setter' was in effect, making something read-only
would be as easy as:

class A {
    var $foo;

    function set_foo($value) {
        // do nothing
    }

Quote:
}
> Unfortunately the developers of Java didn't realize this. It seems though
> like the developers of Python have realized this, at least they have
> introduced what they call properties. Then again, Python is incredibly
cool
> :)

I am becoming a big fan of Python.  It is a much more powerful language than
php, so it is like that chinese saying about mosquitoes and cannnons.  I
don't actually get to use it for development.

However, I've heard of large software developer companies switching to
developing in Python.  The idea is, script your core application in Python -
test for bottlenecks - re-write the slow code as C/C++ extensions to Python.
Leads to rapid development.  Leaves out the compile stage for everything but
slow code.

Besides, as languages go, Python has a few really nice features.

regards,
reggie.



Sat, 23 Apr 2005 02:44:51 GMT  
 Pass variable to variable by reference?

Quote:


>> If we have an object p which is an instance of Person, we can say:
>> d.dob = '1978-07-14'
>> What happens internally though, is this:
>> d.set_dob('1978-07-14')
>> But since set_dob() would be a trivial method which simply updates the
>> dob attribute we won't have to write it.

> i agree except that if we don't use setters then we cannot change our code
> later without updating client code as well (or breaking it). For example,
> let's say that we're making our app y2k compliant, and now need to adjust
> the given 2-digit year: we do that in the setter and nobody knows the
> difference.

My point was that you would be able to change the code. You would simply
define the method as set_dob(), but users of the object wouldn't see this,
they would just access the property dob, which makes a hell of a lot more
sense when you think about it. The point is hiding *represenation*.
set_dob() is the method that you define, but to user set_dob() is the same
is assigning to dob. Likewise, get_dob() is the same as reading the dob
attribute. What I tried to illustrate with the age attribute was an
attribute that wasn't really stored with the object, but when accessed,
using for example d.age, returned the result of the *internal* function
get_age().

Andr N?ss



Sat, 23 Apr 2005 07:08:06 GMT  
 Pass variable to variable by reference?

Quote:

>> If we have an object p which is an instance of Person, we can say:
>> d.dob = '1978-07-14'
>> What happens internally though, is this:
>> d.set_dob('1978-07-14')

> A common naming system for 'setters' and 'getters' would be all that is
> required.

> d.dob = '1978-07-14';

> 1. check to see if a method in the class named set_dob() exists
>     if yes, call it
> 2. check to see if a member variable exists named dob
>     if yes, set it's value
> 3. otherwise create a member variable named dob
>     set it's value

Well something along those lines I guess. However I would be very surprised
if the PHP developers implement something like this, even though it's a
very powerful feature.

Quote:
> However, if the above 'setter' was in effect, making something read-only
> would be as easy as:

> class A {
>     var $foo;

>     function set_foo($value) {
>         // do nothing
>     }
> }

It would, but that's a kludge, what you really wan't is a way to *declare*
the attribute as readonly.

Andr N?ss



Sat, 23 Apr 2005 07:13:33 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Can I pass variables by reference?

2. Passing variables by reference in OS/2 REXX?

3. passing variables by reference.

4. Passing variable by reference to task

5. using the value of a passed by reference variable (g77)

6. passing by reference to variable argument functions

7. Passing variables by reference

8. Paswsing by reference vs passing by variable.

9. Pass variable or class reference to PythonInterpreter?

10. TCL Newbie Question: using variables to reference variables

11. Support for Linking Tcl Variables and C Variables Containing References to Tcl_Obj Structures

12. Character variable variable passed as a subroutine argument

 

 
Powered by phpBB® Forum Software