Current location: Hot Scripts Forums » Programming Languages » PHP » Operator Question...


Operator Question...

Reply
  #1 (permalink)  
Old 08-01-06, 12:27 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Operator Question...

What does "->" do, and "=&" do... I can't seem to find any documentation on these.
Reply With Quote
  #2 (permalink)  
Old 08-01-06, 01:21 AM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
The "->" that you see is the member selection that will be used for object-oriented code using classes.

Such as:
PHP Code:

<?php

class MyClass
{
    function 
someFunction()
    {
        echo 
"Hello World";
    }
}

$object = new MyClass(); // creates object of MyClass
$object->someFunction(); // outputs Hello World
The "=&" is actually a normal = with the & prefixing a variable. Think of it as referring to the memory address of the variable, as opposed to the value itself.

Consider the following: You have a variable $somevar that has been created and assigned to some memory address:
PHP Code:

$somevar "hello"
Now when you go about doing a normal assignment:
PHP Code:

$othervar $somevar
$othervar will equal the value of "hello", but will not be the same as $somevar.

This is illustrated by the code:
PHP Code:

$somevar "hello";

$othervar $somevar;
$othervar "bye";

echo 
"somevar: " $somevar "\n";
echo 
"othervar: " $othervar "\n"
The output would be:
Code:
somevar: hello
othervar: bye
Now consider using the "&":
PHP Code:

$somevar "hello";

$othervar = &$somevar;
$othervar "bye";

echo 
"somevar: " $somevar "\n";
echo 
"othervar: " $othervar "\n"
The output would be:
Code:
somevar: bye
othervar: bye
Notice how the "&" does NOT create a new point in memory but in fact links to the same point as $somevar. Thus, changing $othervar to "bye" will also change $somevar to "bye" and vice versa (actually, they are in fact the same value, but referenced by two different names). Like a person having multiple nicknames, it is the same person, but just referred to in different ways.

Last edited by Patiek; 08-01-06 at 01:24 AM.
Reply With Quote
  #3 (permalink)  
Old 08-01-06, 01:46 AM
duesi's Avatar
duesi duesi is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 225
Thanks: 0
Thanked 0 Times in 0 Posts
Patiek is totally right.

&$somevar is used often to pass a variable by reference to a function,
If you want a function to be able to overwrite the variable (but this ofent leads to bugs which are hard to track).
I use it for example to share a single instance of an object in many other objects:

PHP Code:

//This is an object that can log errors to a logfile

$logger = new logger();

//The Database obejct uses this logger and does not need to 
//create a new instance
$db = new db(&$logger);

//the renderer also
$renderer = new renderer(&$logger); 
Happy Coding!
__________________
Duesi

"One of the great skills in using any language is knowing what not to use, what not to say" (Ron Jeffries)

http://www.swissbytes.de
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 07:23 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.