473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Passing by Reference

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:

Expand|Select|Wrap|Line Numbers
  1. class MyClass {
  2. var $a;
  3. function MyClass(){
  4. $this->a=0;
  5. }
  6.  
  7. function fnClassify($oClass){
  8. $oClass->a = 1;
  9. return true;
  10. }
  11.  
  12. $oMyClass = new MyClass();
  13. echo $oMyClass->a;    // displays "0"
  14.  
  15. $bSuccess = fnClassify($oMyClass);
  16. echo $oMyClass->a;    // displays "1"
  17.  

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?
Sep 11 '07 #1
8 1362
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:

Expand|Select|Wrap|Line Numbers
  1. class MyClass {
  2.     var $a;
  3.     function MyClass(){
  4.         $this->a=0;
  5. }
  6. function fnClassify($oClass){
  7.     $oClass->a = 1;
  8.     return true;
  9. }
  10. $oMyClass = new MyClass();
  11. echo $oMyClass->a;    // displays "0"
  12. $bSuccess = fnClassify($oMyClass);
  13. echo $oMyClass->a;    // displays "1"
  14.  


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
Sep 11 '07 #2
C.
On 11 Sep, 13:20, Sanders Kaufman <bu...@kaufman.netwrote:
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:

Expand|Select|Wrap|Line Numbers
  1. class MyClass {
  2.         var $a;
  3.         function MyClass(){
  4.                 $this->a=0;
  5. }
  6. function fnClassify($oClass){
  7.         $oClass->a = 1;
  8.         return true;
  9. }
  10. $oMyClass = new MyClass();
  11. echo $oMyClass->a;   // displays "0"
  12. $bSuccess = fnClassify($oMyClass);
  13. echo $oMyClass->a;   // displays "1"
  14.  

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.

Sep 11 '07 #3
C. wrote:
On 11 Sep, 13:20, Sanders Kaufman <bu...@kaufman.netwrote:
>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:

Expand|Select|Wrap|Line Numbers
  1. class MyClass {
  2.         var $a;
  3.         function MyClass(){
  4.                 $this->a=0;
  5.         };
  6. }
  7. function fnClassify($oClass){
  8.         $oClass->a = 1;
  9.         return true;
  10. }
  11. $oMyClass = new MyClass();
  12. echo $oMyClass->a;   // displays "0"
  13. $bSuccess = fnClassify($oMyClass);
  14. echo $oMyClass->a;   // displays "1"

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?
Sep 11 '07 #4
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.
Sep 11 '07 #5
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
Sep 11 '07 #6
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.
Sep 11 '07 #7
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
Sep 11 '07 #8
>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 !!!
Sep 11 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
6
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.