Passing by Reference

Passing by Reference

am 11.09.2007 14:20:04 von Bucky Kaufman

I think I finally get the idea of passing by reference, but I'm
struggling with the syntax and grammar of it.

I've got an object and I want to send it to a function to have its
properties modified like so:

[code]
class MyClass {
var $a;
function MyClass(){
$this->a=0;
}

function fnClassify($oClass){
$oClass->a = 1;
return true;
}

$oMyClass = new MyClass();
echo $oMyClass->a; // displays "0"

$bSuccess = fnClassify($oMyClass);
echo $oMyClass->a; // displays "1"
[/code]


Unfortunately, the syntax is wrong somehow. At some point, I should be
passing a reference to, instead of the value of, $oMyClass - but I don't
know where or how.

Can someone help me out here?

Re: Passing by Reference

am 11.09.2007 14:52:33 von gosha bine

On 11.09.2007 14:20 Sanders Kaufman wrote:
> I think I finally get the idea of passing by reference, but I'm
> struggling with the syntax and grammar of it.
>
> I've got an object and I want to send it to a function to have its
> properties modified like so:
>
> [code]
> class MyClass {
> var $a;
> function MyClass(){
> $this->a=0;
> }
>
> function fnClassify($oClass){
> $oClass->a = 1;
> return true;
> }
>
> $oMyClass = new MyClass();
> echo $oMyClass->a; // displays "0"
>
> $bSuccess = fnClassify($oMyClass);
> echo $oMyClass->a; // displays "1"
> [/code]
>
>
> Unfortunately, the syntax is wrong somehow. At some point, I should be
> passing a reference to, instead of the value of, $oMyClass - but I don't
> know where or how.
>
> Can someone help me out here?

In php4 you should prepend & to any variable or a function parameter
containing or refering to an object. Examples include:

function foobar(&$someObject)

$someObj = &new ClassName;

$someObj = &$anotherObj;

Every function that returns an object, should return by reference (use &
twice)

function &getObject() {
....
return &$someObjectVar;
}

Again, all this is needed only for php4. I can't think of a good reason
to use it.


--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: Passing by Reference

am 11.09.2007 14:53:35 von colin.mckinnon

On 11 Sep, 13:20, Sanders Kaufman wrote:
> I think I finally get the idea of passing by reference, but I'm
> struggling with the syntax and grammar of it.
>
> I've got an object and I want to send it to a function to have its
> properties modified like so:
>
> [code]
> class MyClass {
> var $a;
> function MyClass(){
> $this->a=0;
>
> }
>
> function fnClassify($oClass){
> $oClass->a = 1;
> return true;
>
> }
>
> $oMyClass = new MyClass();
> echo $oMyClass->a; // displays "0"
>
> $bSuccess = fnClassify($oMyClass);
> echo $oMyClass->a; // displays "1"
> [/code]
>
> Unfortunately, the syntax is wrong somehow. At some point, I should be
> passing a reference to, instead of the value of, $oMyClass - but I don't
> know where or how.
>
> Can someone help me out here?

I'm confused - do the comments show what you expect to happen? or what
*did* happen?

(the code you've posted shouldn't run - you're missing a '}' from the
method declaration).

IIRC PHP4 will pass the value of an object but PHP5 will pass a
reference to the object unless you specifically tell it to do
something different:

function fnClassify(&$oClass){

C.

Re: Passing by Reference

am 11.09.2007 14:57:19 von Bucky Kaufman

C. wrote:
> On 11 Sep, 13:20, Sanders Kaufman wrote:
>> I think I finally get the idea of passing by reference, but I'm
>> struggling with the syntax and grammar of it.
>>
>> I've got an object and I want to send it to a function to have its
>> properties modified like so:
>>
>> [code]
>> class MyClass {
>> var $a;
>> function MyClass(){
>> $this->a=0;
>> };
>> }
>> function fnClassify($oClass){
>> $oClass->a = 1;
>> return true;
>> }
>> $oMyClass = new MyClass();
>> echo $oMyClass->a; // displays "0"
>> $bSuccess = fnClassify($oMyClass);
>> echo $oMyClass->a; // displays "1"
>> [/code]
>>
>> Unfortunately, the syntax is wrong somehow. At some point, I should be
>> passing a reference to, instead of the value of, $oMyClass - but I don't
>> know where or how.
>>
>> Can someone help me out here?
>
> I'm confused - do the comments show what you expect to happen? or what
> *did* happen?

They show what I *want* to happen.

> (the code you've posted shouldn't run - you're missing a '}' from the
> method declaration).

OK - it's fixed.
But I'm still not sure where to put my "&" thingies.



> IIRC PHP4 will pass the value of an object but PHP5 will pass a
> reference to the object unless you specifically tell it to do
> something different:
>
> function fnClassify(&$oClass){


So... I should put my ByRef "&" in the function definition, not in the
function call - right?

Re: Passing by Reference

am 11.09.2007 15:02:48 von Bucky Kaufman

gosha bine wrote:
> On 11.09.2007 14:20 Sanders Kaufman wrote:

> In php4 you should prepend & to any variable or a function parameter
> containing or refering to an object. Examples include:

"Containing or referring, eh?
So - I should put the byref "&" thing in BOTH the function call AND the
function defintion?
Like thus:
function fnClassify(&$oClass){
$oClass->a = 1;
return true;
}
.... and so:
$bSuccess = fnClassify(&$oMyClass);


> Again, all this is needed only for php4. I can't think of a good reason
> to use it.

That's where I'm at with this thing - 4.

Re: Passing by Reference

am 11.09.2007 15:16:37 von gosha bine

On 11.09.2007 15:02 Sanders Kaufman wrote:
> gosha bine wrote:
>> On 11.09.2007 14:20 Sanders Kaufman wrote:
>
>> In php4 you should prepend & to any variable or a function parameter
>> containing or refering to an object. Examples include:
>
> "Containing or referring, eh?
> So - I should put the byref "&" thing in BOTH the function call AND the
> function defintion?

no, only in the declaration

> Like thus:
> function fnClassify(&$oClass){
> $oClass->a = 1;
> return true;
> }

correct

> ... and so:
> $bSuccess = fnClassify(&$oMyClass);
>

incorrect

>
>> Again, all this is needed only for php4. I can't think of a good
>> reason to use it.
>
> That's where I'm at with this thing - 4.

good luck ;)


--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: Passing by Reference

am 11.09.2007 15:39:22 von Bucky Kaufman

gosha bine wrote:
> On 11.09.2007 15:02 Sanders Kaufman wrote:
>> gosha bine wrote:
>>> On 11.09.2007 14:20 Sanders Kaufman wrote:
>>
>>> In php4 you should prepend & to any variable or a function parameter
>>> containing or refering to an object. Examples include:
>>
>> "Containing or referring, eh?
>> So - I should put the byref "&" thing in BOTH the function call AND
>> the function defintion?
>
> no, only in the declaration
>
>> Like thus:
>> function fnClassify(&$oClass){
>> $oClass->a = 1;
>> return true;
>> }
>
> correct
>
>> ... and so:
>> $bSuccess = fnClassify(&$oMyClass);
>>
>
> incorrect
>
>>
>>> Again, all this is needed only for php4. I can't think of a good
>>> reason to use it.
>>
>> That's where I'm at with this thing - 4.
>
> good luck ;)

T'worked. Thanks. Gimme the name of a charity and maybe I'll Paypal
'em a few bucks.

Re: Passing by Reference

am 11.09.2007 15:58:18 von gosha bine

On 11.09.2007 15:39 Sanders Kaufman wrote:
> gosha bine wrote:
>> On 11.09.2007 15:02 Sanders Kaufman wrote:
>>> gosha bine wrote:
>>>> On 11.09.2007 14:20 Sanders Kaufman wrote:
>>>
>>>> In php4 you should prepend & to any variable or a function parameter
>>>> containing or refering to an object. Examples include:
>>>
>>> "Containing or referring, eh?
>>> So - I should put the byref "&" thing in BOTH the function call AND
>>> the function defintion?
>>
>> no, only in the declaration
>>
>>> Like thus:
>>> function fnClassify(&$oClass){
>>> $oClass->a = 1;
>>> return true;
>>> }
>>
>> correct
>>
>>> ... and so:
>>> $bSuccess = fnClassify(&$oMyClass);
>>>
>>
>> incorrect
>>
>>>
>>>> Again, all this is needed only for php4. I can't think of a good
>>>> reason to use it.
>>>
>>> That's where I'm at with this thing - 4.
>>
>> good luck ;)
>
> T'worked. Thanks. Gimme the name of a charity and maybe I'll Paypal
> 'em a few bucks.

You must be 18+ to do this. Are you?


--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: Passing by Reference

am 11.09.2007 16:46:10 von Steve

>> T'worked. Thanks. Gimme the name of a charity and maybe I'll Paypal 'em
>> a few bucks.
>
> You must be 18+ to do this. Are you?

ROFLMAO !!!