Re: question about objects and references
| From: | Michael Virnstein | Date: | Mon, 13 May 2002 16:06:05 +0000 |
| Subject: | Re: question about objects and references | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-97372@lists.php.net to get a copy of this message | ||
here's an example of what i said:
<?php
class obj {
Var $a = 1;
function byVal() {
byVal($this);
}
function byRef() {
byRef($this);
}
}
function byVal($obj) {
echo $obj->a += 1;
}
function byRef(&$obj) {
echo $obj->a += 1;
}
///////////////////////////////////////////
$a = &new obj();
$a->byVal();
echo "<br>";
echo $a->a;
echo "<br>";
$a->byRef();
echo "<br>";
echo $a->a;
?>
Regards Michael
"Michael Virnstein" <michael.virnstein@brodos.de> schrieb im Newsbeitrag
news:20020513143350.11284.qmail@pb1.pair.com...
>
> "Sascha Mantscheff" <smantscheff@qualimedic.de> schrieb im Newsbeitrag
> news:02051311162204.02523@pico...
>
>
> > When I pass an object as a parameter to a function as "$this", this is
an
> > object reference (according to the docs).
>
> it depends on the function. if you call it by value, not by reference,
> then the function will contain a copy of the original not a pointer.
> Doesn't matter if you call the function with $this or some other
reference.
> $this is a self-reference to the object. you can only use it inside the
> object itself.
> but as soon as you send it to a function as "by value"-parameter, a copy
of
> the
> object will be used inside the function. if you call the function "by
> reference"
> you'll hold a pointer inside the function
>
> > I store this reference in a local variable called $someObject.
$someObject
> > now contains an object pointer.
>
> Ok, let's say the parameter is send to the functions "by reference". Then
> you are
> right, it'll point to the original object, but:
>
> function (&$objRef) {
> $blah = $objRef;
> }
> -> $blah will hold a copy!
>
> function (&$objRef) {
> $blah = &$objRef;
> }
> -> $blah will hold a reference!
>
> > I pass $someObject to another function. Is this a reference to the
> original
> > $this, or is it a copy of the object?
>
> Read what i wrote above and answer it yourself.
>
> Regards Michael.
>
> > All function calls are by value, not by reference (without the "&" name
> > modifier).
> >
> >
> > s.m.
>
>