473,327 Members | 2,112 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,327 software developers and data experts.

references

I'm trying to figure out the correct use of references.

Lets say I have a class Validator and a class User. And I want to let a User
instance be an referenced object in Validator. Where would the best place be
to put the & sign, and why? (See the /* */ comments inside code)

class Validator
{
public function __construct( /* here? */ $caller )
{
$this->class =/* here? */ $caller;
}
}

class User
{
$private validator = NULL;
public function __construct()
{
$this->validator = new Validator( /* or here? */ $this );
}
}

Also, what would happen if were to put & signs in multiple places sprecified
in this particular example? Would it be redundant?

Thank you for any insight.
Apr 12 '07 #1
2 1447
Hi amygdala,

In fact only variables are referenced when using &, not objects or values.

There are four places where you can put the & sign, each has a different
effect:
1. After the = of an assignment,
2. Before the name of a function in the function declaration
3. Before the name of a parameter declaration in a function declaration
4. Before the name of a variable in a function call.

The first makes the assignment assign a reference instead of a value,
The second makes a function return by reference instead of by value,
The third makes a parameter to be passed by reference instead of by
value whenever the function is called
The fourth forces a parameter to be passed by reference only for the
specific function call.

The fourh is depricated and will trigger a warning if
allow_call_time_pass_reference = Off in php.ini.

If you use php5 objects are automatically passed by reference, you don't
need the & sign for that. In php4 there is no choice you can only mimic
object references by referencing variables that hold objects. This can
be tricky, if you can you better use php5.

Using references inapropriately may cause crashes and reference
anomalies*. For this reason php 4.4 and php 5.1 trigger notifications if
a value instead of a variable is assigned or passed by reference. If you
use references consider to use one one of these with error_reporting
set to E_ALL.

Greetings,

Henk Verhoeven,
www.phpPeanuts.org

* http://www.phppeanuts.org/site/index...e+anomaly.html

calling the following and using the result has a good chance to crash php:

function &badOne() {
return $uninitializedVariable;
}

amygdala schreef:
I'm trying to figure out the correct use of references.

Lets say I have a class Validator and a class User. And I want to let a User
instance be an referenced object in Validator. Where would the best place be
to put the & sign, and why? (See the /* */ comments inside code)

class Validator
{
public function __construct( /* here? */ $caller )
{
$this->class =/* here? */ $caller;
}
}

class User
{
$private validator = NULL;
public function __construct()
{
$this->validator = new Validator( /* or here? */ $this );
}
}

Also, what would happen if were to put & signs in multiple places sprecified
in this particular example? Would it be redundant?

Thank you for any insight.

Apr 12 '07 #2
* top posting fixed *

"Henk verhoeven" <news1@phpPeanus_RemoveThis.orgschreef in bericht
news:ev**********@news2.zwoll1.ov.home.nl...
amygdala schreef:
>I'm trying to figure out the correct use of references.

Lets say I have a class Validator and a class User. And I want to let a
User instance be an referenced object in Validator. Where would the best
place be to put the & sign, and why? (See the /* */ comments inside code)

class Validator
{
public function __construct( /* here? */ $caller )
{
$this->class =/* here? */ $caller;
}
}

class User
{
$private validator = NULL;
public function __construct()
{
$this->validator = new Validator( /* or here? */ $this );
}
}

Also, what would happen if were to put & signs in multiple places
sprecified in this particular example? Would it be redundant?

Thank you for any insight.
Hi amygdala,

In fact only variables are referenced when using &, not objects or values.

There are four places where you can put the & sign, each has a different
effect:
1. After the = of an assignment,
2. Before the name of a function in the function declaration
3. Before the name of a parameter declaration in a function declaration
4. Before the name of a variable in a function call.

The first makes the assignment assign a reference instead of a value,
The second makes a function return by reference instead of by value,
The third makes a parameter to be passed by reference instead of by value
whenever the function is called
The fourth forces a parameter to be passed by reference only for the
specific function call.

The fourh is depricated and will trigger a warning if
allow_call_time_pass_reference = Off in php.ini.

If you use php5 objects are automatically passed by reference, you don't
need the & sign for that. In php4 there is no choice you can only mimic
object references by referencing variables that hold objects. This can be
tricky, if you can you better use php5.

Using references inapropriately may cause crashes and reference
anomalies*. For this reason php 4.4 and php 5.1 trigger notifications if a
value instead of a variable is assigned or passed by reference. If you use
references consider to use one one of these with error_reporting set to
E_ALL.

Greetings,

Henk Verhoeven,
www.phpPeanuts.org

*
http://www.phppeanuts.org/site/index...e+anomaly.html

calling the following and using the result has a good chance to crash php:

function &badOne() {
return $uninitializedVariable;
}
I am indeed using PHP 5. I didn't know that objects are referenced by
default. Good to know. Dank je! (Thank you).
Apr 13 '07 #3

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

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
22
by: xmp333 | last post by:
Hi All, I am trying to hide my JavaScript source. The method I chose was to keep all the important source in a password protected folder, and then use a SRC="folder/script.js" to include it...
33
by: JKop | last post by:
I understand variables/objects and pointer variables perfectly: int X = 5; int* pX = &X; *pX = 4; int** ppX = &pX:
2
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
30
by: jeremygetsmail | last post by:
I've got an adp (Metrix.adp) with a reference to another adp (InteractSQL.adp). InteractSQL sits on a server, and is refered to by all of the clients (Metrix), which sit on the client machines...
3
by: DonJefe | last post by:
Does anyone have experience using project->project references in large solutions? What are the plus/minuses that you have found? Currently, we are using the binary assembly references for our...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
3
by: CenturionX | last post by:
Hello everybody: I'd like to know what references in my vba code are used or not. I work in a code made by another person previously, i founded to many references and i believe that someones...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.