473,804 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1379
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(&$someOb ject)

$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(&$oC lass){

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"

Unfortunatel y, 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(&$oC lass){

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(&$oC lass){
$oClass->a = 1;
return true;
}
.... and so:
$bSuccess = fnClassify(&$oM yClass);

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(&$oC lass){
$oClass->a = 1;
return true;
}
correct
... and so:
$bSuccess = fnClassify(&$oM yClass);
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(&$oC lass){
$oClass->a = 1;
return true;
}

correct
>... and so:
$bSuccess = fnClassify(&$oM yClass);

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(&$oC lass){
$oClass->a = 1;
return true;
}

correct
>>... and so:
$bSuccess = fnClassify(&$oM yClass);

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
14960
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10188
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 code... TCHAR myArray; DoStuff(myArray);
13
17937
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 create another Area and assign myArea to it (ie: Area foo = myArea;) or is there a better way? ~AF
8
2118
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, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
11
8134
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
6
6003
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 original value of the local." My code is: using System; using System.Collections.Generic;
12
2691
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
5407
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
7
3314
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
3
2443
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 thought passing by reference, your passing the address in memory. public bool DoSomething(ref byte data) { byte retVal = null; try
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9173
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.