Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 12th, 2007, 10:15 PM
amygdala
Guest
 
Posts: n/a
Default 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.


  #2  
Old April 12th, 2007, 11:45 PM
Henk verhoeven
Guest
 
Posts: n/a
Default Re: references

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:
Quote:
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.
>
>
  #3  
Old April 13th, 2007, 02:45 AM
amygdala
Guest
 
Posts: n/a
Default Re: references

* top posting fixed *

"Henk verhoeven" <news1@phpPeanus_RemoveThis.orgschreef in bericht
news:evmc7c$qdp$1@news2.zwoll1.ov.home.nl...
Quote:
amygdala schreef:
Quote:
>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).


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles