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: -
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"
-
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? 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:
-
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"
-
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
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:
-
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"
-
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.
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:
- 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"
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?
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.
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
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.
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
>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 !!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
|
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);
|
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
|
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:
|
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
| |
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;
|
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);
|
by: Mike |
last post by:
Consider the following code:
"""
struct person {
char *name;
int age;
};
typedef struct person* StructType;
|
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
{
|
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
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |